1 // create template resolver 2 //建立模板解析器能夠用Servlet上下文模板解析器ServletContextTemplateResolver或者類加載模板解析器ClassLoaderTemplateResolver 3 4 ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); 5 ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
建立模板解析器將指定咱們從Servlet上下文檢索模板文件做爲資源,並考慮應用程序的根路徑做爲資源的路徑。以下:html
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」的模板的的時候,將,解析器將會自動加上前綴和後綴(擴展名)。ide
1 //Create Template Engine 2 TemplateEngine templateEngine = new TemplateEngine(); 3 templateEngine.setTemplateResolver(templateResolver); //設置模板解析器
建立模板引擎很簡單,只須要一個模板解析器實例。建立了關鍵的模板解析器和模板引擎以後,咱們就能夠建立模板頁面使用Thymeleaf。ui
模板文件放在'/WEB-INF/templates/'路徑下。要指定DOCTYPE和命名空間。this
<!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>標籤的文本顯示到頁面上。spa
爲了輸出變量「helloworld」的值,咱們須要建立模板上下文,將變量輸出到模板文件中。code
//create servlet context WebContext ctx = new WebContext(req,resp,this.getServletContext(),req.getLocale()); ctx.setVariable('helloword','hello thymeleaf,wellcome!')
執行模板引擎須要傳入模板名、上下文對象以及響應流。以下:xml
//Executing template engine templateEngine.process('home',ctx,resp.getWriter());
讓咱們看看執行模板引擎後的結果:htm
OK,如上所說,模板文件被替換成了標準的XHTML文件了。對象
最後的源碼blog
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()); } }