100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Spring MVC配置多个视图解析器(FreeMarker JSP)

Spring MVC配置多个视图解析器(FreeMarker JSP)

时间:2019-02-11 16:45:26

相关推荐

Spring MVC配置多个视图解析器(FreeMarker JSP)

Spring MVC配置多个视图解析器(FreeMarker,JSP)

Spring MVC开发过程中,有时候需要多个视图解析器策略来解析视图名称,出现这个情况怎么解决?

通过“order”属性来声明优先级,order值越低,则优先级越高。例如:

<beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:p="/schema/p"xmlns:context="/schema/context"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd /schema/mvc /schema/mvc/spring-mvc-3.0.xsd/schema/context /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.monsMultipartResolver"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配置,否则会找不到页面。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。