spring mvc 整合jsp和thymeleaf兩個模板引擎

來源:互聯網
上載者:User

標籤:開啟   run   允許   web   dha   dispatch   let   pack   one   

代碼地址

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

分別用兩個dispatcher分別解析jsp頁面和thymeleaf頁面

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標籤庫 -->    <!-- 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
...    <!-- 配置springmvc -->    <!-- 1.開啟springmvc註解模式 -->    <!-- 簡化配置: 1.自動註冊 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter        org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping        2.提供一系列功能:資料繫結 ,數字日期的format @NumberFormat @DataTimeFormat,xml,json預設讀寫支援 -->    <mvc:annotation-driven/>        <!-- servlet-mapping 映射路徑“/”要做的操作 -->    <!-- 靜態資源預設servlet處理 1.例如 js css 2.允許 / 做整體映射 -->    <mvc:default-servlet-handler/>        <!-- 3.jsp顯示 viewResolver -->    <bean id="jspResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass"                  value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean>            <!-- 掃描web相關的包 -->    <context:component-scan base-package="cn.dxbtech.controller.jsp"/>...

 

src/main/resources/spring-thymeleaf.xml
...    <!-- 配置springmvc -->    <!-- 1.開啟springmvc註解模式 -->    <!-- 簡化配置: 1.自動註冊 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter        org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping        2.提供一系列功能:資料繫結 ,數字日期的format @NumberFormat @DataTimeFormat,xml,json預設讀寫支援 -->    <mvc:annotation-driven/>    <!-- servlet-mapping 映射路徑“/”要做的操作 -->    <!-- 靜態資源預設servlet處理 1.例如 js css 2.允許 / 做整體映射 -->    <mvc:default-servlet-handler/>    <!-- 使用thymeleaf解析 -->    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">        <property name="prefix" value="/WEB-INF/thymeleaf/"/>        <property name="suffix" value=".html"/>        <property name="templateMode" value="HTML"/>        <property name="cacheable" value="false"/>    </bean>    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">        <property name="templateResolver" ref="templateResolver"/>    </bean>    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">        <property name="templateEngine" ref="templateEngine"/>    </bean>    <!-- 掃描web相關的包 -->    <context:component-scan base-package="cn.dxbtech.controller.thymeleaf"/>...

 

src/main/webapp/WEB-INF/web.xml
...    <!--1. 配置 jsp dispatcher -->    <servlet>        <servlet-name>jsp-dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置需要載入的設定檔 -->        <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. 配置 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>...

 

項目結構

運行
mvn tomcat7:run
測試

1.訪問http://localhost:8080/jsp.do

2.訪問http://localhost:8080/thymeleaf.th

spring mvc 整合jsp和thymeleaf兩個模板引擎

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.