Thymeleaf for Spring Boot development applications

Spring Boot provides spring-boot-starter-web to support Web development, and spring-boot-starter-web provides us with embedded Tomcat and Spring MVC dependencies.

 

The startup class in Spring Boot should be placed under the root package. The recommended project structure is:


  • root package structure:com.example
  • The application startup class is Application.javaplaced under the root package, so that when the @ComponentScan annotation is used, the package of the current class is scanned by default
  • static can be used to store static resources
  • templates is used to store the default template configuration path

--- Static files ---

By default, Spring Boot serves static content from a folder on the classpath called /static (/public, /resources or /META-INF/resources) or from the ServletContext root directory. This uses Spring MVC's ResourceHttpRequestHandler, so you can change this behavior (load static files) by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.

In a single web application, the container default servlet is enabled, and if Spring decides not to handle certain requests, the default servlet will load content from the ServletContext root as a fallback (downgrade). Most of the time, this won't happen (unless you modify the default MVC configuration) because Spring can always handle requests through the DispatcherServlet.

Also, an exception to the above standard static resource locations is Webjars content . Any resources in the /webjars/** path will be served from jar files as long as they are packaged in Webjars format.

Note : If your application will be packaged as a jar, then do not use the src/main/webapp folder. Although this folder is a common denominator, it only works when packaged as a war, and if a jar is produced, it is silently ignored by most build tools

 

--- Template Engine---

Spring Boot supports a variety of template engines including:

  • FreeMarker
  • Groovy
  • Thymeleaf (official recommendation)
  • Mustache

JSP technology Spring Boot is officially not recommended for three reasons:

  1. Tomcat only supports the packaging method of war, not executable jar.
  2. Jetty nested containers do not support jsp
  3. Undertow
  4. Creating a custom error.jsp page does not override the default view for error handling, instead a custom error page should be used

When you use any of the above template engines, their default template configuration path is: src/main/resources/templates. Of course, this path can also be modified.

 

--- Thymeleaf Template Engine---

Thymeleaf is a template engine for rendering XML/XHTML/HTML5 content. Similar to JSP, Velocity, FreeMaker, etc., it can also be easily integrated with Web frameworks such as Spring MVC as a template engine for Web applications. Compared with other template engines, the biggest feature of Thymeleaf is that it can directly open and display template pages in the browser without starting the entire web application. Its functional characteristics are as follows:

  • The method in @Controller in Spring MVC can directly return the template name, and then the Thymeleaf template engine will automatically render
  • Expressions in templates support Spring Expression Language (Spring EL)
  • Form support and compatible with Spring MVC's data binding and validation mechanism
  • International support

Spring also recommends the use of Thymeleaf, so this code integration uses Thymeleaf to integrate.

 

--- Import dependencies---

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


如图所示,spring-boot-starter-thymeleaf会自动包含spring-boot-starter-web,所以我们就不需要单独引入web依赖了。

 

http://tengj.top/2017/03/13/springboot4/#

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326349859&siteId=291194637