Spring MVC多视图配置(一)

2014-11-24 08:20:17 · 作者: · 浏览: 5

[java]
1.Spring配置的XML
[html]
< xml version="1.0" encoding="UTF-8" >

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"
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 ">
base-package="com.spring.action"/>








class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">





class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">





class="com.spring.action.MultiViewResover">


jsp">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
value="org.springframework.web.servlet.view.JstlView"/>





class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">






class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">









2 解析多视图请求的类

[java]
package com.spring.action;

import java.util.Locale;
import java.util.Map;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

/**
* @author Jeson
* @blog:http://www.gbsou.com
* @date:Oct 20, 2009 8:39:27 AM
* @version :1.0
*
*/
public class MultiViewResover implements ViewResolver {

private Map resolvers;

@Override
public View resolveViewName(String viewName, Locale locale)
throws Exception {
int n = viewName.lastIndexOf("_"); // 获取
// viewName(modelAndView中的名字)看其有没有下划线
if (n == (-1))
return null; // 没有则直接返回空
// 有的话截取下划线后面的字符串 这里一般是jsp,ftl,vm与配置文件中的的key匹配
String suffix = viewName.substring(n + 1);
// 根据下划线后面的字符串去获取托管的视图解析类对象
ViewResolver resolver = resolvers.get(suffix);

// 取下划线前面的部分 那时真正的资源名.比如我们要使用hello.jsp 那viewName就应该是hello_jsp