Problem Description
In my mind, it's the dominant use of thymeleaf, because I've always wanted to know what Springboot recommended. In the ordinary Springboot project, the direct introduction of the thymeleaf of the dependency package can be a view resolution, and in the general Spring project requires us to do some manual configuration, so there is this problem, how to integrate multiple template engine together. Solutions Introduce thymeleaf dependencies
<!--https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-->
<dependency>
<groupid >org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${ thymeleaf.version}</version>
</dependency>
<!--https://mvnrepository.com/artifact/ Org.thymeleaf/thymeleaf-spring3-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>${thymeleaf.version}</ Version>
</dependency>
Thymeleaf versions 2 and 3 are OK, and the 3 feature is a bit more perfect.
In addition Tymeleaf-spring3 or Thymeleaf-spring4 is also free to use.
2. Configuration SPRINGMVC View Diagram analysis
The key is this step, this step online version is more. I've probably analyzed it. There are two main situations: the first situation JSP pages and HTML pages are located in a different view folder under the second case two kinds of pages exist in the same view folder
First look at the configuration of the first scenario:
Here you can refer to the official thymeleaf of a pet store where the configuration address is
Thymeleaf Official Configuration Example
Here is a sample of us:
<bean id= "Templateresolver" class= "Org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver"
> <property name= "prefix" value= "web-inf/"/> <property name= "suffix" value= ". html"/>
<property name= "Templatemode" value= "HTML5"/> <property name= "cacheable" value= "false"/> <property name= "characterencoding" value= "UTF-8"/> </bean> <bean id= "Templateengine" cl ass= "Org.thymeleaf.spring3.SpringTemplateEngine" > <property name= "templateresolver" ref= "Templateresolver" > </bean> <!--Configure multiple views to resolve--> <bean class= "Org.springframework.web.servlet.view.ContentNegotia Tingviewresolver "> <property name=" viewresolvers "> <list> <!--used
Thymeleaf--> <bean class= "Org.thymeleaf.spring3.view.ThymeleafViewResolver" > <property name= "ChaRacterencoding "value=" UTF-8 "/> <property name=" templateengine "ref=" Templateengine "/> <property name= "Viewnames" value= "html/*"/> <property name= "Order" value= "2"/ > </bean> <!--used jsp--> <bean class= "Org.springframe Work.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf/"/&"
Gt
<property name= "suffix" value= ". jsp"/> <property "name=" Viewnames "value=" jsp/* <property name= "Order" value= "1"/> </bean> </list> ;/property> </bean>
Key points:
<property name= "prefix" value= "/web-inf/"/>
<property name= "Viewnames" value= "*"/>
Next, the configuration of the second scenario:
Which extends from the first configuration, the prefix values are the same.
<bean id= "Templateresolver" class= "Org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver" > <property name= "prefix" value= "web-inf/views/"/> <property name= "Templatemode" value= "HTML5"/&
Gt <property name= "cacheable" value= "false"/> <property name= "characterencoding" value= "UTF-8"/> ;/bean> <bean id= "Templateengine" class= "Org.thymeleaf.spring3.SpringTemplateEngine" > < Property Name= "Templateresolver" ref= "Templateresolver"/> </bean> <!--Configure multiple views resolution--> <bean C lass= "Org.springframework.web.servlet.view.ContentNegotiatingViewResolver" > <property name= "viewresolvers" > <list> <!--used thymeleaf--> <bean class= "Org.thymeleaf.
Spring3.view.ThymeleafViewResolver "> <property name=" characterencoding "value=" UTF-8 "/>" <property name= "Templateengine" ref= "Templateengine"/> <property name= "Viewnames" * . html "/> <property name=" Order "value=" 1 "/> </bean> & lt;!
--Used JSP--> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/web-inf/views/"/> <property name= "Viewnam"
Es "value=" *.jsp "/> <property name=" Order "value=" 2 "/> </bean> </list> </property> </bean>
Different places are here:
<property name= "Viewnames" value= "*.html"/>
<property name= "Viewnames" value= "*.jsp"/>
It's not over yet, how we're going to return at the controller level.
The first case return "JSP/ABC" | | Return "THYMELEAF/ABC"
The second case return "abc.jsp" | | Return "abc.html"