为了避免代码用于,写一个类THUtils用于管理Thymeleaf
package utils;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
public class THUtils {
private static TemplateEngine te;
static {
te = new TemplateEngine();
ClassLoaderTemplateResolver r = new ClassLoaderTemplateResolver();
r.setCharacterEncoding("utf-8");
te.setTemplateResolver(r);
}
public static void print(String fileName,Context context,HttpServletResponse response) throws IOException {
String html = te.process(fileName, context);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(html);
out.close();
}
}
写一个测试类,用于测试THUtils
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
public class Th1Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//使用Thymeleaf模板引擎讲th1.html输出
//1.实例化模板引擎对象
TemplateEngine te = new TemplateEngine();
//2.实例化一个模板引擎配置对象
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
//3.设置字符集
resolver.setCharacterEncoding("utf-8");
//4.将配置在模板引擎上应用
te.setTemplateResolver(resolver);
//5.创建上下文对象,页面上显示的内容
Context context = new Context();
//6.将数据放入到context中
context.setVariable("name", "关云长");
//7.使用模板引擎,结合页面上要呈现的数据,生成页面副本的字符串
String html = te.process("th1.html", context);
//将页面副本的字符串输出
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(html);
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
用于测试的网页模板th1.html;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf1</title>
</head>
<body>
<ul>
<li>张飞</li>
<li th:text="${name}">关羽</li>
<li>刘备</li>
<li>曹操</li>
</ul>
</body>
</html>
写一个测试类,用于更改模板中的值
package controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.context.Context;
import utils.THUtils;
public class Th4Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Person p1 = new Person("吕布","男",30);
Person p2 = new Person("貂蝉","女",22);
Person p3 = new Person("孙尚香","女",20);
List<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
Context context = new Context();
context.setVariable("list", list);
THUtils.print("th3.html", context, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
用于测试的网页模板th3.html;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- th:each 遍历集合的用法 注:p代表集合中的每一个对象 -->
<ul th:each="p : ${list}">
<li th:text="${p.name}">曹操</li>
<li th:text="${p.gender}">男</li>
<li th:text="${p.age}">49</li>
</ul>
<!-- th:remove 删除当前标签以及内部的所有内容 -->
<ul th:remove="all">
<li>曹操</li>
<li>男</li>
<li>49</li>
</ul>
<ul>
<li>刘备</li>
<li>男</li>
<li>50</li>
</ul>
</body>
</html>