Spring MVC : Java模板引擎 Thymeleaf (一)

时间:2022-11-17 16:40:30

在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的清单:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
<artifactId>gs-serving-web-content</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.5.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<properties>
<start-class>hello.Application</start-class>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestone</id>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-milestone</id>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

</project>

按照maven规范,新建目录:

└── 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";
}

}

Main函数,

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

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + ' spring mvc !'" />
</body>
</html>


这里的一些注解的含义这里不再介绍(之前笔者翻译的Spring文档有过说明)。

执行 mcn clean package

再执行 target下的可执行jar文件。

接着就能在浏览器访问了。下一篇详细介绍thymeleaf的用法。