The main research in this article is about the use of the Thymeleaf template engine in SpringMVC, which is introduced as follows.
Thymeleaf provides a set of Spring integrations that allow you to use them as a comprehensive alternative to JSP in Spring MVC applications.
<!-- thymeleaf-spring4 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.6.RELEASE</version> </dependency>
@Beanpublic SpringResourceTemplateResolver templateResolver(){ // SpringResourceTemplateResolver automatically integrates with Spring itself // Resource solution infrastructure, highly recommended. SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(this.applicationContext); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); // HTML is the default value, added here for clarity. templateResolver.setTemplateMode(TemplateMode.HTML); // By default, the template cache is true. If you want to set to false // The template is automatically updated when modified. templateResolver.setCacheable(true); return templateResolver;}@Beanpublic SpringTemplateEngine templateEngine(){ // SpringTemplateEngine automatically applies SpringStandardDialect // and enable Spring's own MessageSource message resolution mechanism. SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); // Enable SpringEL compiler with Spring 4.2.4 or later // can speed up execution in most cases, but when the expressions of // in a template are reused between different data types, // may be incompatible with specific cases, so the flag defaults to "false" // for safer backward compatibility. templateEngine.setEnableSpringELCompiler(true); return templateEngine;}<!-- SpringResourceTemplateResolver automatically integrates with Spring itself --> <!-- Resource solution infrastructure, highly recommended. --> <bean id="templateResolver" > <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".html" /> <!-- HTML is the default value, added here for clarity. --> <property name="templateMode" value="HTML" /> <!-- By default, the template cache is true. If you want to set to false --> <!-- The template is automatically updated when modified. --> <property name="cacheable" value="true" /> </bean> <!-- SpringTemplateEngine automatically applies SpringStandardDialect and --> <!-- Use Spring's own MessageSource message resolution mechanism. --> <bean id="templateEngine"> <property name="templateResolver" ref="templateResolver" /> <!-- Enable Spring EL compiler with Spring 4.2.4 or later --> <!-- can speed up execution in most cases, but when expressions of --> <!-- in a template are reused between different data types, --> <!-- may be incompatible with specific cases, so the flag defaults to "false" --> <!-- for safer backward compatibility. --> <property name="enableSpringELCompiler" value="true" /> </bean>
@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 View and view solver 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;}<bean> <property name="templateEngine" ref="templateEngine" /> <!-- Note that "order" and "viewNames" are optional --> <property name="order" value="1" /> <property name="viewNames" value="*.html,*.xhtml" /></bean>
The above is all the content of this article about the use of Thymeleaf template engine instance code in SpringMVC. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!