第一種方式:
在基於SpringMVC的項目中有時需要同時使用多種視圖格式,如jsp,velocity及freemarker等,通過不同的請求路徑配置規則,映射到不同的視圖檔案。下面我提供一種思路,通過視圖模板檔案尾碼名的方式來進行處理。例如:
@RequestMapping(value = "/hello")
public String jspTest(HttpServletRequest request, ModelMap map) {
return "demo.jsp";
}
將通過”demo.jsp”的尾碼自動使用jsp的視圖解析器。
下面描述下詳細思路:
一.自訂視圖解析器
package com.jeedev.common.web.springmvc.view;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.Set;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.web.servlet.View;import org.springframework.web.servlet.ViewResolver;/** * 自訂視圖解析(通過配置實現多視圖整合,如jsp,velocity,freemarker,pdf,excel...) * @author huligong * */public class JeeDevViewResolver implements ViewResolver { private static Log logger = LogFactory.getLog(JeeDevViewResolver.class); public View resolveViewName(String viewName, Locale locale) throws Exception { for(Map.Entry<Set<String>, ViewResolver> map : viewResolverMap.entrySet()){ Set<String> suffixs = map.getKey(); for(String suffix : suffixs){ if (viewName.endsWith(suffix)){ ViewResolver viewResolver = map.getValue(); if(null != viewResolver){ if (logger.isDebugEnabled()) { logger.debug("found viewResolver '" + viewResolver + "' for viewName '" + viewName+ "'"); } return viewResolver.resolveViewName(viewName, locale); } } } } if(defaultViewResolver != null){ return defaultViewResolver.resolveViewName(viewName, locale); } // to allow for ViewResolver chaining return null; } private Map<Set<String>,ViewResolver> viewResolverMap = new HashMap<Set<String>,ViewResolver>(); private ViewResolver defaultViewResolver = null; public Map<Set<String>, ViewResolver> getViewResolverMap() { return viewResolverMap; } public void setViewResolverMap(Map<Set<String>, ViewResolver> viewResolverMap) { this.viewResolverMap = viewResolverMap; } public ViewResolver getDefaultViewResolver() { return defaultViewResolver; } public void setDefaultViewResolver(ViewResolver defaultViewResolver) { this.defaultViewResolver = defaultViewResolver; }}
在自訂視圖解析器(其實是視圖中轉器)中,通過對視圖檔案的尾碼判斷(而不是請求地址的尾碼)而返回給不同的視圖解析器處理。
二.視圖映射配置dispather-servlet.xml
<bean id="viewResolver" class="com.jeedev.common.web.springmvc.view.JeeDevViewResolver"> <property name="defaultViewResolver" ref="beanNameViewResolver"/> <property name="viewResolverMap"> <map> <entry> <key> <set> <value>.jsp</value> </set> </key> <ref bean="jspViewResolver"/> </entry> <entry> <key> <set> <value>.vm</value> <value>.htm</value> </set> </key> <ref bean="velocityViewResolver"/> </entry> <entry> <key><value>.ftl</value></key> <ref bean="freeMarkerViewResolver"/> </entry> </map> </property> </bean> <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <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"/>--> </bean> <!-- Velocity ViewResolver Configuration --> <bean id="velocityViewResolver" class = "org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="order" value="0" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="requestContextAttribute" value="req"/> </bean> <!-- Velocity Configuration --> <bean id="velocityConfig" class = "org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="configLocation" value="/WEB-INF/velocity.properties"/> <property name="resourceLoaderPath" value="/&