Spring MVC integrates JSP and thymeleaf two template engines

Source: Internet
Author: User

Code address

Http://github.com/dingxiaobo/multi-view-resolver

Parse JSP pages and thymeleaf pages separately with two dispatcher

Pom.xml
...    <!--Thymeleaf -    <!--Https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -    <Dependency>        <groupId>Org.thymeleaf</groupId>        <Artifactid>Thymeleaf</Artifactid>        <version>3.0.6.RELEASE</version>    </Dependency>    <!--Https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -    <Dependency>        <groupId>Org.thymeleaf</groupId>        <Artifactid>Thymeleaf-spring4</Artifactid>        <version>3.0.6.RELEASE</version>    </Dependency>    <!--JSP Tag Library -    <!--Https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -    <Dependency>        <groupId>Javax.servlet.jsp.jstl</groupId>        <Artifactid>Javax.servlet.jsp.jstl-api</Artifactid>        <version>1.2.1</version>    </Dependency>...

Src/main/resources/spring-jsp.xml
...    <!--Configure SPRINGMVC -    <!--1. Turn on SPRINGMVC annotation mode -    <!--Simplified configuration: 1. Autoenrollment Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter Org.springframewor K.web.servlet.mvc.annotation.defaultannotationhandlermapping 2. Provides a range of functions: Data binding, format @NumberFormat for digital dates @DataTimeFo Rmat,xml,json default read/write support -    <Mvc:annotation-driven/>        <!--servlet-mapping Map Path "/" action to do -    <!--static Resource default servlet handles 1. For example JS CSS 2. Allow/do overall mapping -    <Mvc:default-servlet-handler/>        <!--3.jsp Display Viewresolver -    <BeanID= "Jspresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView"/>        < Propertyname= "prefix"value= "/web-inf/jsp/"/>        < Propertyname= "suffix"value= ". jsp"/>    </Bean>            <!--Scan web-related packages -    <Context:component-scanBase-package= "cn.dxbtech.controller.jsp"/>...

Src/main/resources/spring-thymeleaf.xml
...    <!--Configure SPRINGMVC -    <!--1. Turn on SPRINGMVC annotation mode -    <!--Simplified configuration: 1. Autoenrollment Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter Org.springframewor K.web.servlet.mvc.annotation.defaultannotationhandlermapping 2. Provides a range of functions: Data binding, format @NumberFormat for digital dates @DataTimeFo Rmat,xml,json default read/write support -    <Mvc:annotation-driven/>    <!--servlet-mapping Map Path "/" action to do -    <!--static Resource default servlet handles 1. For example JS CSS 2. Allow/do overall mapping -    <Mvc:default-servlet-handler/>    <!--using thymeleaf parsing -    <BeanID= "Templateresolver"class= "Org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">        < Propertyname= "prefix"value= "/web-inf/thymeleaf/"/>        < Propertyname= "suffix"value= ". html"/>        < Propertyname= "Templatemode"value= "HTML"/>        < Propertyname= "cacheable"value= "false"/>    </Bean>    <BeanID= "Templateengine"class= "Org.thymeleaf.spring4.SpringTemplateEngine">        < Propertyname= "Templateresolver"ref= "Templateresolver"/>    </Bean>    <Beanclass= "Org.thymeleaf.spring4.view.ThymeleafViewResolver">        < Propertyname= "Templateengine"ref= "Templateengine"/>    </Bean>    <!--Scan web-related packages -    <Context:component-scanBase-package= "Cn.dxbtech.controller.thymeleaf"/>...

Src/main/webapp/web-inf/web.xml
...    <!--1. Configure JSP Dispatcher -    <servlet>        <Servlet-name>Jsp-dispatcher</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <!--Configure the configuration files that need to be loaded -        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>Classpath:spring-jsp.xml</Param-value>        </Init-param>        <Load-on-startup>0</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Jsp-dispatcher</Servlet-name>        <Url-pattern>*.do</Url-pattern>    </servlet-mapping>    <!--2. Configure Thymeleaf Dispatcher -    <servlet>        <Servlet-name>Thymeleaf-dispatcher</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>Classpath:spring-thymeleaf.xml</Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Thymeleaf-dispatcher</Servlet-name>        <Url-pattern>*.th</Url-pattern>    </servlet-mapping>...

Project structure

Run
mvn tomcat7:run
Test

1. Visit http://localhost:8080/jsp.do

2. Visit http://localhost:8080/thymeleaf.th

Spring MVC integrates JSP and thymeleaf two template engines

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.