Thymeleaf template engine uses
What is Thymeleaf
Thymeleaf is a Java Library. It is a xml/xhtml/html5 template engine that can apply a set of transformations on a template file, displaying the data or text generated by the program to a template file.
Thymeleaf -dependent jar Packages
To use Thymeleaf, we need to introduce the relevant jar in the classpath path of our Web app , as follows:
Thymeleaf-2.1.3.release.jar
Ognl-3.0.6.jar
Javassist-3.16.1-ga.jar
Unbescape-1.0.jar
Servlet-api-2.5.jar
Slf4j-api-1.6.1.jar
Slf4j-log4j12-1.6.1.jar
Log4j-1.2.15.jar
Mail-1.4.jar
Activation-1.1.jar
Creating a template Parser
The premise of using Thymeleaf is very simple, first create a template parser to load the template, as follows:
1 // 2 Servletcontexttemplateresolver Templateresolver = new 3 Classloadertemplateresolver templateresolver = new Classloadertemplateresolver ();
Create template parser can be used servlet servletcontexttemplateresolver classloadertemplateresolver servlet, and considers the application's root path as the path to the resource." as follows:
// XHTML is the default mode, but we'll set it anyway for better understanding of codeTemplateresolver.settemplat Emode ("XHTML"); // This would convert "home" to "/web-inf/templates/home.html"templateresolver.setprefix ("/web-inf/templates/" ); Templateresolver.setsuffix (". html");
As shown above, a template node is created through the parser, and when you use Thymeleaf to render a template named "home", the parser will automatically prefix and suffix (extension).
Creating a template engine
Once you have created a template parser to load the template, then you need to create a template engine to do the actual processing, as follows:
// Create Template Engine New templateengine (); Templateengine.settemplateresolver (templateresolver);
Creating a template engine is very close to single and requires only one instance of the template parser. After creating the critical template Parser and template engine, we can create a template page using Thymeleaf.
Create a template
Our template file is placed under the /web-inf/templates/ path. Creating a template file requires only specifying Thymeleaf specific DOCTYPE and Namespaces . As follows:
<!DOCTYPE html SYSTEM "HTTP://WWW.THYMELEAF.ORG/DTD/XHTML1-STRICT-THYMELEAF-4.DTD"><HTMLxmlns= "http://www.w3.org/1999/xhtml"xmlns:th= "http://www.thymeleaf.org"> <Head> <title>Hello Thymeleaf</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> </Head> <Body> <P> <spanTh:text= "#{home.welcome}">This is text won't is show</span> </P> </Body></HTML>
As shown, a "th:text" attribute is also defined in the template file, andwe think the template file is valid because we have declared the Thymeleaf DTD through DOCTYPE , this property is declared in this class library. When this template is processed, this "th:text" attribute will be removed and the template file will be replaced with a strict standard XHTML file.
The value of the Th:textproperty in the template fileis a variable expression thattakes the value of the variable "hellword" as a <span> The text of the label is displayed on the page.
Creating a template context
In order to displaythe value of the variable "hellword", we need to create the template context and output the variable to the template file. Such as:
// Create Servlet Context New This . Getservletcontext (), Req.getlocale ()), ctx.setvariable ("Helloword", "Hello thymeleaf,wellcome!");
Executing the template engine
Everything is available, but the engine does not fire. All we need to do now is execute the template engine, which needs to pass in the template name, context object, and response flow. As follows:
// executing template enginetemplateengine.process ("Home", CTX, Resp.getwriter ());
Let's look at the results after executing the template engine:
OK, as stated above, the template file is replaced with the standard XHTML file.
For more information please visit the official website http://www.thymeleaf.org/.
Attached Source:
Public classThymeleafservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; @Overrideprotected voiddoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {doPost (REQ,RESP); } @Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//Create Template ResolverServletcontexttemplateresolver Templateresolver =NewServletcontexttemplateresolver (); //classloadertemplateresolver templateresolver = new Classloadertemplateresolver (); //XHTML is the default mode, but we'll set it anyway for better understanding of codeTemplateresolver.settemplatemode ("XHTML"); //This would 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 LRUTemplateresolver.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 EngineTemplateengine Templateengine =NewTemplateengine (); Templateengine.settemplateresolver (Templateresolver); //Write The response headersResp.setcontenttype ("Text/html;charset=utf-8"); Resp.setheader ("Pragma", "No-cache"); Resp.setheader ("Cache-control", "No-cache"); Resp.setdateheader ("Expires", 0); //Create Servlet ContextWebcontext CTX =NewWebcontext (req, RESP, This. Getservletcontext (), Req.getlocale ()); Ctx.setvariable ("Helloword", "Hello thymeleaf,wellcome!"); //Executing template engineTemplateengine.process ("Home"), CTX, Resp.getwriter ()); }}
Thymeleaf template engine uses