springmvc同时映射jsp,和velocity,在controller中只需返回一个view的名字,然后先去velocity中找,找到就返回,找不到就去jsp中找。找的先后顺序是通过order配置的。
dispatcher-servlet.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 增加对annotation的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/productList/fuzzySearch/*/*.html" />
<bean id="testInterceptor" class="com.web.frontpage.mvc.intercrptor.TestInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 自动扫描controll -->
<context:component-scan base-package="com.web.frontpage.controller"></context:component-scan>
<!-- jsp视图映射 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
<property name="order" value="1"></property>
</bean>
<!-- velocity配置 -->
<!-- velocity的配置 -->
<bean id="velocityConfigure"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="configLocation" value="classpath:velocity/velocity.properties"></property>
<property name="resourceLoaderPath" value="/WEB-INF/vm/"></property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8></prop>
</props>
</property>
</bean>
<!--spring 对velocity文件的映射 -->
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".vm"></property>
<property name="exposeRequestAttributes" value="true"></property>
<property name="exposeSessionAttributes" value="true"></property>
<property name="contentType" value="text/html;charset=UTF-8"></property>
<property name="order" value="0"></property>
</bean>
<!-- 增加对json的支持 -->
<bean id="convereJson"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean id="anotationAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="convereJson" />
</list>
</property>
</bean>
<!-- 增加对上传的支持 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1024000"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
velocity.properties内容如下:
resource.loader=webapp
webapp.resource.loader.class=org.apache.velocity.tools.view.servlet.WebappLoader
webapp.resource.loader.path=/WEB-INF/vm/
input.encoding=UTF-8
output.encoding=UTF-8