當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring MVC配置多个视图解析器(FreeMarker,JSP)
生活随笔
收集整理的這篇文章主要介紹了
Spring MVC配置多个视图解析器(FreeMarker,JSP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring MVC配置多個視圖解析器(FreeMarker,JSP)
Spring MVC開發過程中,有時候需要多個視圖解析器策略來解析視圖名稱,出現這個情況怎么解決?
通過“order”屬性來聲明優先級,order值越低,則優先級越高。例如:
?
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 自動掃描com.baobaotao.web 包下的@Controller標注的類控制器類 --><context:component-scan base-package="com.feng.web" /><!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 --><mvc:annotation-driven/><!-- 對模型視圖名稱的解析,在請求時模型視圖名稱添加前后綴 --><!-- FreeMarker基礎設施及視圖解析器配置 --><beanclass="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"p:templateLoaderPath="/WEB-INF/ftl" p:defaultEncoding="UTF-8"><property name="freemarkerSettings"><props><prop key="classic_compatible">true</prop></props></property></bean><beanclass="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"p:order="0" p:suffix=".ftl" p:contentType="text/html; charset=utf-8" /> <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:order="1" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"p:defaultEncoding="utf-8" /><bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"p:basename="i18n/messages" /><!--WEB異常解析處理--><bean id="exceptionResolver" class="com.baobaotao.web.ForumHandlerExceptionResolver"><property name="defaultErrorView"><value>fail</value></property><property name="exceptionMappings"><props><prop key="java.lang.RuntimeException">fail</prop></props></property></bean> </beans>
注意:
1:InternalResourceViewResolver必須賦予給最低的優先級(最大的order值),因為不管返回什么視圖名稱,它都將解析視圖。如果它的優先級高于其它解析器的優先級的話,它將使得其它具有較低優先級的解析器沒有機會解析視圖。
2:如果Controller中返回視圖加了后綴jsp或者ftl,在配置中就不要加入suffix配置,否則會找不到頁面。
總結
以上是生活随笔為你收集整理的Spring MVC配置多个视图解析器(FreeMarker,JSP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FreeMarker四种变量的用法
- 下一篇: gradle idea java ssm