在Java世界的MVC框架里,使用的视图技术不少,最基本的是JSP,还有知名的FreeMarker和Velocity等模板引擎。Thymeleaf也是一款优秀的模板引擎,它在HTML5/XHTML的视图层表现的很好,也能在离线情况下处理任何XML文件。它是完全可以替代JSP+JSTL的。
下面是来自于Thymeleaf官方的Q&A:
Q: 和FreeMarker,Velocity相比,Thymeleaf表现得怎样呢?
A:FreeMarker和Velocity都是软件领域杰出的作品,但它们在解决模板问题上的处理哲学和Thymeleaf不一样。
Thymeleaf强调的是自然模板,也就是允许模板作为产品原型使用(笔者注:因为其后缀名就是.html,可以直接在浏览器打开),而FreeMarker和Velocity不行。并且Thymeleaf的语法更简洁、更和目前Web开发的趋势相一致。其次,从架构的角度看,FreeMarker和Velocity更像个文本处理器,所以它们能够处理很多类型的内容,而Thymeleaf是基于XML的,只能处理XML格式的数据。因此这样看,FreeMarker和Velocity更通用些,但恰恰如此,Thymeleaf更能利用XML的特性,尤其是在Web应用中。
下面就以Spring官方的例子为基础,给出一个使用Thymeleaf的基本MVC实现:(使用Maven构建)
首先是pom.xml的清单:
4.0.0
org.springframework
gs-serving-web-content
0.1.0
org.springframework.boot
spring-boot-starter-parent
1.1.5.RELEASE
org.springframework.boot
spring-boot-starter-thymeleaf
hello.Application
org.springframework.boot
spring-boot-maven-plugin
spring-milestone
http://repo.spring.io/libs-release
spring-milestone
http://repo.spring.io/libs-release
└── src
└── main
└── java
└── hello
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class TestController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
package hello;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
└── main
└── resources
└── regreeting.html
Getting Started: Spring MVC
这里的一些注解的含义这里不再介绍(之前笔者翻译的Spring文档有过说明)。
执行 mcn clean package
再执行 target下的可执行jar文件。
接着就能在浏览器访问了。下一篇详细介绍thymeleaf的用法。