Template engine Thymeleaf

1.Thymeleaf Profile

Is a Thymeleaf with Velocity, FreeMarker similar template engine, it can completely replace the JSP. Compared with other template engines, it has the following three very attractive features

  • Thymeleaf Jieke run under the environment of network and non-network that it allows artists to see the effect of static page in a browser, but also allows the programmer to see the effect of dynamic pages with data on the server. This is because it supports html prototype, and then add additional attributes to achieve in the html tag template + display of the data. The browser will ignore the interpretation of undefined html tag attributes, so thymeleaf templates can be statically run; when there is data to return to the page, Thymeleaf label dynamically replace static content, dynamic display the page.
  • Thymeleaf box-use characteristics. It provides standard and Spring standard two dialects, can directly apply a template to achieve JSTL, OGNL expressions effect, to avoid daily sets of templates, change JSTL, plagued change the label. At the same time developers can extend and create custom dialect.
  • Spring Thymeleaf provide standard dialect and a perfect integration with SpringMVC optional module, you can quickly achieve a form of binding, property editor, international and other functions. Is a Thymeleaf with Velocity, FreeMarker similar template engine, it can completely replace the JSP. Compared with other template engines, it has the following three very attractive features
    • Thymeleaf Jieke run under the environment of network and non-network that it allows artists to see the effect of static page in a browser, but also allows the programmer to see the effect of dynamic pages with data on the server. This is because it supports html prototype, and then add additional attributes to achieve in the html tag template + display of the data. The browser will ignore the interpretation of undefined html tag attributes, so thymeleaf templates can be statically run; when there is data to return to the page, Thymeleaf label dynamically replace static content, dynamic display the page.
    • Thymeleaf box-use characteristics. It provides standard and Spring standard two dialects, can directly apply a template to achieve JSTL, OGNL expressions effect, to avoid daily sets of templates, change JSTL, plagued change the label. At the same time developers can extend and create custom dialect.
    • Spring Thymeleaf provide standard dialect and a perfect integration with SpringMVC optional module, you can quickly achieve a form of binding, property editor, international and other functions.

This project is a first jar SpringBoot way, not war, like the second, we still use the embedded Tomcat, so, what he is now the default is not supported by the jsp.

It does not support jsp, if the way we use pure static pages, it gives us direct development will bring great trouble, how to do it, SpringBoot recommend you to use a template engine.

Thymeleaf 官 网: https: //www.thymeleaf.org/

Thymeleaf home in the Github: https: //github.com/thymeleaf/thymeleaf

Location Thymeleaf template classpath:/templates/under

2. Why Thymeleaf

If you want to publish Jar form module is try not to use JSP knowledge, this is because the JSP Servlet container embedded in the run have some problems (embedded Tomcat, Jetty do not support forms Jar running JSP , Undertow does not support JSP).

Spring Boot Thymeleaf recommended for use as a template engine, because Thymeleaf provide the perfect support for Spring MVC

Spring Boot provides a number of template engine, including:

  • FreeMarker
  • Groovy
  • Mustache
  • Thymeleaf
  • Velocity
  • Beetl (bee template engine)

3. Use Thymeleaf

(1) introducing dependent

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

or

<!--thymeleaf模板-->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

(2) write controller

@RequestMapping("/test")
public String test(){
    return "test";
}

(3) written pages, ending with html, and under the resources / thymeleaf folder

Guess you like

Origin www.cnblogs.com/sq-bmw/p/11988423.html