SpringMVC 配置多视图解析器(velocity,jsp)
1.自定义视图解析器
package com.zhaochao.controller; 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 zhaochao * */ 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){ System.out.println(suffix+" == "+viewName); 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+".jsp", 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; } }
2.SpringMVC配置
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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" default-autowire="byName"> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.zhaochao.controller" /> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 --> </list> </property> </bean> <bean id="viewResolver" class="com.zhaochao.controller.JeeDevViewResolver"> <property name="defaultViewResolver" ref="jspViewResolver" /> <property name="viewResolverMap"> <map> <entry> <key> <set> <value>.vm</value> </set> </key> <ref bean="velocityViewResolver" /> </entry> <entry> <key> <set> <value>jsp</value> </set> </key> <ref bean="jspViewResolver" /> </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="/WEB-INF/vm/" /> <property name="velocityProperties"> <props> <prop key="input.encoding">UTF-8</prop> <prop key="output.encoding">UTF-8</prop> </props> </property> </bean> </beans>
3.Controller控制器
package com.zhaochao.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import com.zhaochao.bean.user.UserBean; import com.zhaochao.service.user.UserService; @Controller @RequestMapping("/userAction") public class UserController { private UserService userService; public UserService getUserService() { return userService; } @Autowired public void setUserService(UserService userService) { this.userService = userService; } @RequestMapping(value = "/def") public String def(HttpServletRequest request, ModelMap map) { map.put("name", "hello world"); request.setAttribute("name", "hello wrod!"); List<UserBean> users=userService.getUserEntities(); map.put("name", "hello world"); map.put("users", users); return "def"; } @RequestMapping(value = "/jsp") public String test1(HttpServletRequest request, ModelMap map) { map.put("name", "hello world"); request.setAttribute("name", "hello wrod!"); List<UserBean> users=userService.getUserEntities(); map.put("name", "hello world"); map.put("users", users); return "index.jsp"; } @RequestMapping(value = "/vm") public String test3(HttpServletRequest request, ModelMap map) { List<UserBean> users=userService.getUserEntities(); map.put("name", "hello world"); map.put("users", users); return "index.vm"; } }
4.View层目录结构
5.效果
完整项目下载地址
http://download.csdn.net/detail/whzhaochao/8631177