Thymeleaf template engine instance code is used in SpringMVC

  • 2021-01-25 07:33:56
  • OfStack

This paper mainly studies the Thymeleaf template engine in SpringMVC, the details are as follows.

Thymeleaf provides a set of Spring integrations, allowing you to use them as a comprehensive replacement for JSP in Spring MVC applications.

Maven rely on


    <!-- thymeleaf-spring4 -->
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring4</artifactId>
      <version>3.0.6.RELEASE</version>
    </dependency>

Configure the template parser

JavaConfig method:


@Bean
public SpringResourceTemplateResolver templateResolver(){
  // SpringResourceTemplateResolver Automatic and Spring Their integration 
  //  Resources address infrastructure ,  Highly recommended. 
  SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
  templateResolver.setApplicationContext(this.applicationContext);
  templateResolver.setPrefix("/WEB-INF/templates/");
  templateResolver.setSuffix(".html");
  // HTML Is the default ,  For the sake of clarity ,  Add here. 
  templateResolver.setTemplateMode(TemplateMode.HTML);
  //  By default ,  The template cache is true . If you want to set to false
  //  Templates are automatically updated as they are modified. 
  templateResolver.setCacheable(true);
  return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine(){
  // SpringTemplateEngine Automatic application SpringStandardDialect
  //  And enable the Spring Their own MessageSource Message resolution mechanism. 
  SpringTemplateEngine templateEngine = new SpringTemplateEngine();
  templateEngine.setTemplateResolver(templateResolver());
  //  use Spring 4.2.4 Or later SpringEL The compiler 
  //  Speed up execution in most cases ,  But when 1 A template 
  //  Is reused between different data types , 
  //  May not be compatible with specific situations ,  So the flag defaults to" false " 
  //  For more secure backward compatibility. 
  templateEngine.setEnableSpringELCompiler(true);
  return templateEngine;
} 

XML way


  <!-- SpringResourceTemplateResolver Automatic and Spring Their integration  -->
  <!--  Resources address infrastructure ,  Highly 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 ,  For the sake of clarity ,  Add here.  -->
    <property name="templateMode" value="HTML" />
    <!--  By default ,  The template cache is true . If you want to set to false -->
    <!--  Templates are automatically updated as they are modified.  -->
    <property name="cacheable" value="true" />
  </bean>

  <!-- SpringTemplateEngine Automatic application SpringStandardDialect and  -->
  <!--  use Spring Their own MessageSource Message resolution mechanism.  -->
  <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
    <!--  use Spring 4.2.4 Or later Spring EL The compiler  -->
    <!--  Speed up execution in most cases ,  But when 1 A template  -->
    <!--  Is reused between different data types , -->
    <!--  May not be compatible with specific situations ,  So the flag defaults to" false "  -->
    <!--  For more secure backward compatibility.  -->
    <property name="enableSpringELCompiler" value="true" />
  </bean>

Views and view resolvers in Thymeleaf


@Bean
public ThymeleafViewResolver viewResolver(){
  ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
  viewResolver.setTemplateEngine(templateEngine());
  //  Note that" order "And" viewNames "Is optional 
  viewResolver.setOrder(1);
  viewResolver.setViewNames(new String[] {".html", ".xhtml"});
  return viewResolver;
} 
13420.2 Thymeleaf View and view resolver in the 
 @Bean
public ThymeleafViewResolver viewResolver(){
  ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
  viewResolver.setTemplateEngine(templateEngine());
  //  Note that" order "And" viewNames "Is 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 "Is optional  -->
 <property name="order" value="1" />
 <property name="viewNames" value="*.html,*.xhtml" />
</bean> 

conclusion

This article is about SpringMVC using Thymeleaf template engine example code all content, hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: