SpringMVC uses the Thymeleaf template engine instance code, springmvcthymeleaf
This article focuses on the Thymeleaf template engine used in SpringMVC.
Thymeleaf provides a set of Spring integration that allows you to use it as a full replacement of JSP in Spring MVC applications.
Maven dependency
<!-- thymeleaf-spring4 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.6.RELEASE</version> </dependency>
Configure the template parser JavaConfig:
@ Beanpublic SpringResourceTemplateResolver templateResolver () {// SpringResourceTemplateResolver automatically integrates with Spring itself // resources to address infrastructure, which is strongly recommended. SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver (); templateResolver. setApplicationContext (this. applicationContext); templateResolver. setPrefix ("/WEB-INF/templates/"); templateResolver. setSuffix (". html "); // HTML is the default value. For clarity, add it here. TemplateResolver. setTemplateMode (TemplateMode. HTML); // by default, the template cache is true. If you want to set it to false // The template is automatically updated upon modification. TemplateResolver. setCacheable (true); return templateResolver;} @ Beanpublic SpringTemplateEngine templateEngine () {// SpringTemplateEngine automatically applies SpringStandardDialect // and enables Spring's MessageSource message parsing mechanism. SpringTemplateEngine templateEngine = new SpringTemplateEngine (); templateEngine. setTemplateResolver (templateResolver (); // use Spring 4.2.4 or later to enable the SpringEL compiler // to speed up execution in most cases, however, when the expressions of // In a template are duplicated between different data types, // may be incompatible with specific situations, therefore, this flag defaults to "false" // to achieve more secure backward compatibility. TemplateEngine. setEnableSpringELCompiler (true); return templateEngine ;}XML Method
<! -- SpringResourceTemplateResolver automatically integrates with Spring itself --> <! -- Resource solution infrastructure, which is strongly recommended. --> <Bean id = "templateResolver" class = "org. thymeleaf. spring4.templateresolver. springResourceTemplateResolver "> <property name =" prefix "value ="/WEB-INF/views/"/> <property name =" suffix "value = ". html "/> <! -- HTML is the default value. For clarity, add it here. --> <Property name = "templateMode" value = "HTML"/> <! -- By default, the template cache is true. If you want to set it to false --> <! -- The template is automatically updated when it is modified. --> <Property name = "cacheable" value = "true"/> </bean> <! -- SpringTemplateEngine automatically applies SpringStandardDialect and --> <! -- Use Spring's MessageSource message parsing mechanism. --> <Bean id = "templateEngine" class = "org. thymeleaf. spring4.SpringTemplateEngine"> <property name = "templateResolver" ref = "templateResolver"/> <! -- Enable Spring EL compiler using Spring 4.2.4 or later --> <! -- Can speed up execution in most cases, but when a template --> <! -- When the expression is used between different data types, --> <! -- This flag may be incompatible with a specific situation, so the default value of this flag is "false" --> <! -- To achieve more secure backward compatibility. --> <Property name = "enableSpringELCompiler" value = "true"/> </bean>
View and view splitter in Thymeleaf
@ Beanpublic ThymeleafViewResolver viewResolver () {ThymeleafViewResolver viewResolver = new ThymeleafViewResolver (); viewResolver. setTemplateEngine (templateEngine (); // note that "order" and "viewNames" are optional viewResolver. setOrder (1); viewResolver. setViewNames (new String [] {". html ",". xhtml "}); return viewResolver;} 13420.2 views and views in Thymeleaf @ Beanpublic extends viewResolver () {ThymeleafViewResolver viewResolver = new ThymeleafViewResolver (); viewResolver. setTemplateEngine (templateEngine (); // note that "order" and "viewNames" are optional viewResolver. setOrder (1); viewResolver. setViewNames (new String [] {". html ",". xhtml "}); return viewResolver ;}Or in XML format:
<Bean class = "org. thymeleaf. spring4.view. ThymeleafViewResolver"> <property name = "templateEngine" ref = "templateEngine"/> <! -- Note that "order" and "viewNames" are optional --> <property name = "order" value = "1"/> <property name = "viewNames" value = "*. html ,*. xhtml "/> </bean>
Summary
The above is all the content about the Thymeleaf template engine instance code in SpringMVC. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!