hellowordmonkey 2014-05-11
在使用springmvc集成jsp和freemarker时,我们要在spriingmvc的配置文件中同时配置sp的解析器和freemarker的解析器,同时解析器order属性(order越小,优先级别越高),指定视图的解析优先级,于是配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.lyl.controller"></context:component-scan>
<!-- jsp视图解析器 -->
<bean id="jspViewResolver" 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"/>
<property name="order" value="1"></property>
<property name="cache" value="false"></property>
</bean>
<!-- FreeMarker环境配置 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- freemarker模板位置 -->
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>
<!-- FreeMarker视图解析 -->
<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".xhtml"/>
<property name="order" value="0"></property>
</bean>
</beans>
红色字体部分,需要注意,一定要设置freemarker的优先级别高于jsp,这样当找不到相应的freemarker页面时,还会继续找jsp页面,否则,当jsp页面不存在,会报404错误!