Springboot integration and common mistakes Thymeleaf

  Thymeleaf template engine is springboot in the default configuration, and freemarker similar, can completely replace jsp, in springboot in its default path is src / main / resources / templates static files css, js and other documents the default path is src / main / resources / static, if not all of the items in this directory need to manually add the

First we need to add a dependency in pom.xml file

<!-- thymeleaf 模板引用  -->
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>  </dependency>

  

After the quote we have to test, after the introduction of dependence in pom.xml. You can not configure (also adhering springboot convention over configuration) Of course, if you need custom attributes, you can add a configuration in application.properties in.

Test class @Controller

/**
 * @author pillarzhang
 * @date 2019-06-03
 */
@Controller
public class loginController {
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

  

Index, html page as follows

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<p style="color:red">hello world</p>
</body>
</html>

  

Startup Items, enter http: // localhost: 8081 / index to see the following page

This successful integration of the Thymeleaf.

 

Note: The front also said, if you do not configure any properties still can be used, of course, you can also set up their own, application.properties set the properties in the configuration file

spring.thymeleaf.prefix = classpath: / templates / default path provided thymeleaf the src / main / Resources / Templates 
spring.thymeleaf.suffix = .html template provided thymeleaf suffix 
spring.thymeleaf.content-type = text / HTML 
spring.thymeleaf. cache = false cache is turned on by default to true 
spring.thymeleaf.mode = LEGACYHTML5 provided thymeleaf strict check 
spring.thymeleaf.encoding = UTF-8 encoding is provided

  

  1. After configuration must pay attention to the path address is correct,
  2. Must use @Controller, if you use @RestController, it is possible to return a string of characters in return
  3. Do not add @ResponseBody former method, add the comment is equivalent @RestController, returns a string string above
  4. If the carrier application.properties reconfiguration property, you must pay attention to whether the writing is wrong, otherwise there is no more space may report the following error:

     

  So far, springboot integrated thymeleaf is complete, although the middle encountered some small problems, but fortunately resolved.

 

  If inappropriate and mistakes, please point out that we communicate with learning and common progress! Thank you!

Guess you like

Origin www.cnblogs.com/zhang-dongliang/p/10968878.html