在servlet中使用thymeleaf
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
对于一个应用, 只需要配置一次Thymeleaf就可以了, 每次请求都可以使用同一个TemplateEngine实例. 可以使用单例模式.
package cn.lacknb.thymeleaf;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import javax.servlet.ServletContext;
/*
* 对于一个应用, 只需要配置一次Thymeleaf就可以了. 每次请求
* 都可以使用同一个TemplateEngine实例. 可以使用单例模式
* 代码如下:
* */
public class WebApplication {
private TemplateEngine templateEngine;
private static WebApplication webApplication;
/*
* 私有构造器
* */
private WebApplication(ServletContext servletContext){
// 创建模板解析器
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
// 设置模板模式为HTML
templateResolver.setTemplateMode(TemplateMode.HTML);
// 设置前缀与后缀
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
this.templateEngine = new TemplateEngine();
this.templateEngine.setTemplateResolver(templateResolver);
}
public static WebApplication getInstance(ServletContext servletContext){
if (webApplication == null){
webApplication = new WebApplication(servletContext);
}
return webApplication;
}
public TemplateEngine getTemplateEngine(){
return this.templateEngine;
}
}
在WebApplication类的构造器中, 对TemplateEngine进行了配置, 并使用ServletContextTemplateResolver作为模板解析器, 分别配置了模板模式, 处理时的前缀与后缀. 在Servlet中, 只需要调用WebApplication的getTemplateEngine方法就可以获得TemplateEngine的实例.
注意: WebApplication使用单例模式, 在单例模式下, 需要提供一个私有的构造器, 一个返回自己实例的方法.
package cn.lacknb.servlet;
import cn.lacknb.beans.User;
import cn.lacknb.thymeleaf.WebApplication;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class UserServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
WebApplication webapp = WebApplication.getInstance(this.getServletContext());
// 设置用户数据
// thymeleaf中context
Context context = new Context();
context.setVariable("users", getUsers());
// 获取模板引擎实例
TemplateEngine engine = webapp.getTemplateEngine();
String result = engine.process("user", context);
resp.setContentType("text/html; charset=utf-8");
// 输出渲染后的HTML
resp.getWriter().print(result);
// 新建一个user.html, 在WEB-INF/templates/
}
private List<User> getUsers(){
List<User> users = new ArrayList<User>();
users.add(new User(1, "测试1"));
users.add(new User(2, "测试2"));
users.add(new User(3, "测试3"));
users.add(new User(4, "测试4"));
users.add(new User(5, "测试5"));
return users;
}
}
user.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="http://66.lacknb.cn/ssm/js/jquery3.3.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</head>
<body>
<table align="center" class="table">
<tr>
<td>序号</td>
<td>姓名</td>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
</tr>
</table>
</body>
</html>