JFinal Template Engine 采用独创的DKFF(Dynamic Key Feature Forward)词法分析算法以及独创的DLRD(Double Layer Recursive Descent)语法分析算法,极大减少了代码量,降低了学习成本,并提升了用户体验。与以往任何一款java模板引擎都有显著的不同,极简设计、独创算法、极爽开发体验,从根本上重新定义了模板引擎。
JFinal模板引擎专为java开发者打造,所以坚持两个核心设计理念:一是在模板中可以直接与java代码通畅地交互,二是尽可能沿用java语法规则,将学习成本降到极致。
因此,立即掌握 90% 的用法,只需要记住一句话:JFinal 模板引擎表达式与java是直接打通的。摘至官方文档
由于该模板引擎达式与java是直接打通的,很多语法一看就明白了,所以直接上例子再配合官方文档进行阅读,立即就掌握了。
此项目源码地址jfinal-demo-04-enjoy-Template
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.perfree</groupId>
<artifactId>jfinal-demo-04-enjoy-Template</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jetty-server</artifactId>
<version>2018.11</version>
<!--
此处的 scope 值为 compile 仅为支持 IDEA 下启动项目
打 war 包时需要改成 provided,以免将一些无用的 jar 打进去
-->
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
<build>
<finalName>jfinal-demo-03-controller</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- java8 保留参数名编译参数,支持 action 带参 -->
<compilerArgument>-parameters</compilerArgument>
<compilerArguments>
<verbose />
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>jfinal-demo-04-enjoy-Template</display-name>
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>com.perfree.common.DemoConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
com.perfree.common包下DemoConfig类继承JFinalConfig:
package com.perfree.common;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.template.Engine;
import com.perfree.controller.EnjoyController;
public class DemoConfig extends JFinalConfig {
public static void main(String[] args) {
JFinal.start("src/main/webapp", 80, "/",5);
}
@Override
public void configConstant(Constants me) {
me.setDevMode(true);
}
@Override
public void configEngine(Engine me) {
}
@Override
public void configHandler(Handlers me) {
// TODO Auto-generated method stub
}
@Override
public void configInterceptor(Interceptors me) {
// TODO Auto-generated method stub
}
@Override
public void configPlugin(Plugins me) {
// TODO Auto-generated method stub
}
@Override
public void configRoute(Routes me) {
me.add("/", EnjoyController.class,"/");
}
}
com.perfree.pojo包下User类
package com.perfree.pojo;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
com.perfree.controller包下EnjoyController类继承Controller
package com.perfree.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jfinal.core.Controller;
import com.perfree.pojo.User;
public class EnjoyController extends Controller {
public void index() {
//传入对象
User user = new User();
user.setName("铁蛋");
user.setAge(22);
setAttr("user", user);
//向前台传入数字,前台作判断是否等于1,若等于1,展示哈哈,等于0展示呵呵,其他啦啦啦
setAttr("num", 1);
//向前台传入map
Map<String,String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
setAttr("map", map);
//向前台传入list
List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王二");
setAttr("names", list);
//向前台传入空值
setAttr("para", null);
render("/index.html");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<!-- 普通字符串展示 -->
<h4>普通字符串展示:</h4>
#("Hello~")<br>
hello+2=#("hello"+2)<br>
将hello前两个字母打印: #("hello".substring(0, 2))<br>
<hr>
<!-- 普通运算 -->
<h4>普通运算:</h4>
1+1=#(1+1)<br>
1==1:#(1==1)<br>
三元运算:#(1==1?"相等":"不相等")
<hr>
<!-- 从对象取值 -->
<h4>从对象取值:</h4>
#(user.name)
#(user.age)
#(user.getName())
#(user.getAge())
<hr>
<!-- if条件判断 -->
<h4>if条件判断: </h4>
#if(num == 0)
num等于0,呵呵
#else if(num == 1)
num等于1,哈哈
#else
num不等于0也不等于1,啦啦啦
#end
<hr>
<!-- 循环从list取值,names为后台传来的参数 -->
<h4>循环从list取值: </h4>
#for(name:names)
#(name)
#end
<hr>
<!-- 从map取值 -->
<h4>从map取值: </h4>
key1: #(map["key1"])<br>
key2: #(map.key2)<br>
key1: #(map.get("key1"))<br>
所有的key: #(map.keySet())<br>
遍历map:
#for(key:map.keySet())
#(map.get(key))
#end
<hr>
<!-- 定义map并输出 -->
<h4>定义map并输出: </h4>
#set(myMap={"张三":22,"李四":21})
#(myMap)<br>
#(myMap.put("王二",20))
#(myMap)<br>
<hr>
<!-- 空合并安全取值调用操作符 -->
<h4>空合并安全取值调用操作符: </h4>
#(para ??)它是空值
<hr>
</body>
</html>
最后,该模板引擎表达式与Java是直接打通的,你能想到的,尽可以自己试验~官方文档地址