thymeleaf 模板引擎
1.创建模板解析器 Create Template Resolver 用来加载模板
1 // create template resolver 2 //创建模板解析器可以用Servlet上下文模板解析器ServletContextTemplateResolver或者类加载模板解析器ClassLoaderTemplateResolver 3 4 ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); 5 ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
创建模板解析器将指定我们从Servlet上下文检索模板文件作为资源,并考虑应用程序的根路径作为资源的路径。如下:
1 // XHTML is the default mode, but we will set it anyway for better understanding of code 2 templateResolver.setTemplateMode('XHTML'); 3 4 // This will convert "home" to "/WEB-INF/templates/home.html" 5 templateResolver.setPrefix('/WEB-INF/templates/'); 6 templateResolver.setSuffix('.html');
如上所示,通过解析器创建模板节点,当使用Thymeleaf渲染名为“home”的模板的的时候,将,解析器将会自动加上前缀和后缀(扩展名)。
2.创建模板引擎 Create Template Engine
1 //Create Template Engine 2 TemplateEngine templateEngine = new TemplateEngine(); 3 templateEngine.setTemplateResolver(templateResolver); //设置模板解析器
创建模板引擎很简单,只需要一个模板解析器实例。创建了关键的模板解析器和模板引擎之后,我们就可以创建模板页面使用Thymeleaf。
3.创建模板文件
模板文件放在'/WEB-INF/templates/'路径下。要指定DOCTYPE和命名空间。
<!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> <title>Hello thymeleaf</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p> <span th:text="#{home.welcome}">this is text will not be show</span> </p> </body> </html>
模板文件中的th:text”属性的值是一个变量表达式,它将获取变量“hellword”的值作为<span>标签的文本显示到页面上。
4.创建模板上下文
为了输出变量“helloworld”的值,我们需要创建模板上下文,将变量输出到模板文件中。
//create servlet context WebContext ctx = new WebContext(req,resp,this.getServletContext(),req.getLocale()); ctx.setVariable('helloword','hello thymeleaf,wellcome!')
5.执行模板引擎
执行模板引擎需要传入模板名、上下文对象以及响应流。如下:
//Executing template engine templateEngine.process('home',ctx,resp.getWriter());
让我们看看执行模板引擎后的结果:
OK,如上所说,模板文件被替换成了标准的XHTML文件了。
最后的源码
public class thymeleafServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Create Template Resolver ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); //ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); // XHTML is the default mode, but we will set it anyway for better understanding of code templateResolver.setTemplateMode("XHTML"); // This will convert "home" to "/WEB-INF/templates/home.html" templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); // Set template cache TTL to 1 hour. If not set, entries would live in cache until expelled by LRU templateResolver.setCacheTTLMs(Long.valueOf(3600000L)); // Cache is set to true by default. Set to false if you want templates to // be automatically updated when modified. templateResolver.setCacheable(true); //Create Template Engine TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); //Write the response headers resp.setContentType("text/html;charset=UTF-8"); resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); //Create Servlet context WebContext ctx = new WebContext(req, resp, this.getServletContext(), req.getLocale()); ctx.setVariable("helloword","hello thymeleaf,wellcome!"); //Executing template engine templateEngine.process("home", ctx, resp.getWriter()); } }