I. Understanding and Analysis
Spring MVC defines an interface called Viewresolver, which is roughly as follows:
Public Interface viewresolver { throws Exception;}
View is another interface.
Public Interface View { String getcontenttype (); void throws Exception;}
The task of the view interface is to accept the model and the request and response objects of the servlet, and render the output to response.
All we need to do is write the Viewresolver and view implementations, put the rendered content into the response, and show it to the user's browser. In fact, Spring provides a number of built-in implementations.
Second, the use of thymeleaf
1. Configuring the Thymeleaf View Resolver
In order to use thymeleaf in spring, we need to configure three thymeleaf-enabled beans that are integrated with spring:
- Thymeleafviewresolver: Resolves the logical view name to the Thymeleaf template view;
- Springtemplateengine: Working with templates and rendering results;
- Templateresolver: Loads the thymeleaf template.
The following is a Java configuration that declares these beans.
Previous versions of THYMELEAF3:
@Bean Publicviewresolver viewresolver (springtemplateengine templateengine) {thymeleafviewresolver ViewResolver=NewThymeleafviewresolver (); Viewresolver.settemplateengine (Templateengine); returnViewresolver;} @Bean Publictemplateengine templateengine (templateresolver templateresolver) {springtemplateengine TemplateEngine=NewSpringtemplateengine (); Templateengine.settemplateresolver (Templateresolver); returnTemplateengine;} @Bean Publictemplateresolver Templateresolver () {templateresolver templateresolver=NewServletcontexttemplateresolver (); Templateresolver.setprefix ("/web-inf/templates/"); Templateresolver.setsuffix (". html"); Templateresolver.settemplatemode ("HTML5"); returnTemplateresolver;}
THYMELEAF3 version:
@Bean Publicviewresolver viewresolver (springtemplateengine templateengine) {thymeleafviewresolver ViewResolver=NewThymeleafviewresolver (); Viewresolver.settemplateengine (Templateengine); returnViewresolver;} @Bean Publictemplateengine templateengine (templateresolver templateresolver) {springtemplateengine TemplateEngine=NewSpringtemplateengine (); Templateengine.settemplateresolver (Templateresolver); returnTemplateengine;} @Bean Publicitemplateresolver Templateresolver () {templateresolver templateresolver=NewSpringresourcetemplateresolver (); Templateresolver.setprefix ("/web-inf/templates/"); Templateresolver.setsuffix (". html"); Templateresolver.settemplatemode ("HTML5"); returnTemplateresolver;}
If you prefer to use XML to configure the Bean, the following <bean> declaration can accomplish the task.
<bean id= "Viewresolver" class= "Org.thymeleaf.spring3.view.ThymeleafViewResolver" P: Templateengine-ref= "templateengine"/><bean id= "Templateengine" class= " Org.thymeleaf.spring3.SpringTemplateEngine " p:templateresolver-ref=" Templateresolver "/>< Bean id= "Templateresolver" class= "Org.thymeleaf.templateresolver.ServletContextTemplateResolver" P:prefix= "/web-inf/templates" p:suffix= ". html" p:templatemode = "HTML5"/>
2. Defining thymeleaf Templates
Thymeleaf is to a large extent HTML file, unlike JSP, it has no special tag or tag library. Thymeleaf works because it adds the Thymeleaf property to the standard set of HTML tags through a custom named control.
[Notes] "spring Combat"-rendering Web View