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.xsd
           http://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基础设施及视图解析器配置 -->
	<bean
		class="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>

	<bean
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
		p:order="0" p:suffix=".ftl" p:contentType="text/html; charset=utf-8" />	
	  	  
	<bean
		class="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配置,否则会找不到页面。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值