Java EE MVC Thymeleaf
最后修改于 2020 年 7 月 13 日
Java EE MVC Thymeleaf 教程展示了如何将 Thymeleaf 模板引擎集成到 Java EE MVC Web 框架中。我们使用 Tomcat Web 服务器。
Thymeleaf
Thymeleaf 是一个现代化的服务器端 Java 模板引擎,适用于 Web 和独立环境。
在 Web 应用程序中,Thymeleaf 旨在成为 JSP 的完整替代品。它是一个自然的模板引擎:模板文件可以直接在浏览器中打开,并且仍然能够正确地显示为网页。
Java EE MVC
Java MVC 是一项规范 (JSR-371),用于新的基于动作的 Java Web 框架。它是传统基于组件的 JSF 的替代方案。MVC API 构建在 JAX-RS 之上,并与现有的 Java EE 技术(如 CDI 和 Bean Validation)集成。Eclipse Ozark 是 Java MVC 的一种实现。它目前支持 RESTEasy、Jersey 和 Apache CXF。
Java MVC Thymeleaf 示例
在下面的 Web 应用程序中,我们创建了一个简单的 Java MVC 应用程序。它使用 Thymeleaf 显示数据。
$ tree
.
├── nb-configuration.xml
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── zetcode
│ │ ├── config
│ │ │ └── ApplicationConfig.java
│ │ ├── controller
│ │ │ ├── CityController.java
│ │ │ └── HelloController.java
│ │ └── model
│ │ ├── City.java
│ │ └── Message.java
│ ├── resources
│ └── webapp
│ ├── index.html
│ ├── META-INF
│ │ └── context.xml
│ └── WEB-INF
│ ├── beans.xml
│ └── views
│ ├── cities.html
│ └── hello.html
└── test
└── java
这是项目结构。
<?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>com.zetcode</groupId>
<artifactId>JavaMVCThymeleaf</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JavaMVCThymeleaf</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mvc-spec.ozark</groupId>
<artifactId>ozark-resteasy</artifactId>
<version>1.0.0-m03</version>
</dependency>
<dependency>
<groupId>javax.mvc</groupId>
<artifactId>javax.mvc-api</artifactId>
<version>1.0-pr</version>
</dependency>
<dependency>
<groupId>org.mvc-spec.ozark</groupId>
<artifactId>ozark-core</artifactId>
<version>1.0.0-m03</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.5.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>3.5.1.Final</version>
</dependency>
<dependency>
<groupId>org.mvc-spec.ozark.ext</groupId>
<artifactId>ozark-thymeleaf</artifactId>
<version>1.0.0-m03</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是 Maven POM 文件。它包含了构建我们的应用程序所需的所有必要库,包括 Java MVC 框架、Weld 容器和 Thymeleaf 引擎。
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/JavaMVCThymeleaf">
<Resource name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>
在 Tomcat 的 context.xml 文件中,我们定义了上下文路径并注册了 Weld 的 BeanManager 工厂。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
在 WEB-INF 目录中,我们有一个空的 beans.xml 文件。它是 CDI 的部署描述符。它可以用于配置拦截器、装饰器和其他内容。即使我们不做任何配置,也需要添加一个空的 beans.xml 来注册 CDI。
package com.zetcode.config;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("mvc")
public class ApplicationConfig extends Application {
}
ApplicationConfig 是应用程序配置类。由于 Servlet 3.0,可以省略 web.xml 文件。在 JAX-RS 中,我们创建一个扩展抽象 Application 的配置类,并使用 @ApplicationPath 注解。Application 定义了 JAX-RS 应用程序的组件并提供额外的元数据。在这里,我们注册应用程序所需的资源类、提供程序或属性。
package com.zetcode.model;
import java.util.Objects;
public class City {
private Long id;
private String name;
private int population;
public City(Long id, String name, int population) {
this.id = id;
this.name = name;
this.population = population;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + Objects.hashCode(this.id);
hash = 97 * hash + Objects.hashCode(this.name);
hash = 97 * hash + this.population;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final City other = (City) obj;
if (this.population != other.population) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
@Override
public String toString() {
return "City{" + "id=" + id + ", name=" + name
+ ", population=" + population + '}';
}
}
City bean 包含城市对象的数据。它有三个属性:id、name 和 population。
package com.zetcode.model;
import javax.enterprise.context.RequestScoped;
@RequestScoped
public class Message {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
这是一个包含消息的简单模型。
@RequestScoped
public class Message {
定义为 @RequestScoped 的对象为每个请求创建一次,并在请求期间由所有注入它的 bean 共享。
package com.zetcode.controller;
import com.zetcode.model.Message;
import javax.inject.Inject;
import javax.mvc.Models;
import javax.mvc.annotation.Controller;
import javax.mvc.annotation.View;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("hello")
@Controller
public class HelloController {
@Inject
private Message message;
@Inject
private Models models;
@GET
@Produces("text/html")
@View("hello.html")
public void hello() {
message.setText("Today is a sunny day");
models.put("message", message);
}
}
HelloController 向客户端返回一条简单的消息。
@Path("hello")
@Controller
public class HelloController {
@Controller 注解定义了一个控制器,@Path 注解定义了到控制器的路由。
@GET
@Produces("text/html")
@View("hello.html")
public void hello() {
@GET 注解定义了调用 hello 方法的方法。@Produces 注解定义了响应的媒体类型。@View 注解定义了视图名称。它是 Thymeleaf 模板文件的名称。
message.setText("Today is a sunny day");
models.put("message", message);
我们设置消息并将消息 bean 放入 models 对象。model 包含由视图显示的数据。
package com.zetcode.controller;
import com.zetcode.model.City;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.mvc.Models;
import javax.mvc.annotation.Controller;
import javax.mvc.annotation.View;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("cities")
@Controller
public class CityController {
@Inject
private Models models;
@GET
@Produces("text/html")
@View("cities.html")
public void getCities() {
List<City> cities = new ArrayList<>();
cities.add(new City(1L, "Bratislava", 432000));
cities.add(new City(2L, "Budapest", 1759000));
cities.add(new City(3L, "Prague", 1280000));
cities.add(new City(4L, "Los Angeles", 1748000));
cities.add(new City(5L, "New York", 3971000));
models.put("cities", cities);
}
}
CityController 返回一个城市列表。
<html>
<head>
<title>Message</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p th:text="${message.text}">Message text</span>
</body>
</html>
hello.html 是第一个 Thymeleaf 模板。模板位于默认的 WEB-INF/views 目录中。
<p th:text="${message.text}">Message text</span>
使用 Thymeleaf 的 th:text="${message.text}" 指令显示 bean 中的文本。
<!DOCTYPE html>
<html>
<head>
<title>Cities</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Cities</h2>
<ul>
<li th:each="c : ${cities}" th:text="${c.name + ': ' + c.population}">city</li>
</ul>
</body>
</html>
在第二个视图文件中,我们使用 Thymeleaf 的 th:each 指令在 HTML 列表中显示城市列表。
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<meta charset="UTF-8">
</head>
<body>
<p>
<a href="mvc/hello">Get message</a>
</p>
<p>
<a href="mvc/cities">Get cities</a>
</p>
</body>
</html>
这是主页。它包含两个链接,用于在服务器上执行两个操作。
在本教程中,我们展示了如何将 Thymeleaf 集成到 Java EE MVC 框架中。