SpringMVC自定义多视图解析器

实现通过请求url后缀判断返回值类型, 例如 localhost:8080/list 返回JSP页面, localhost:8080/list.json返回json格式数据

1.创建一枚举返回值类型

 public enum ResultType { REDIRECT, JSP, JSON, XML } 

2.创建自定义的viewResolver

public class JsonViewResolver implements ViewResolver {

    private View view;

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        return this.view;
    }

}

 

public class XmlViewResolver implements ViewResolver {

    private View view;

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        return this.view;
    }

}

 

public class MultipleViewResolver extends WebApplicationObjectSupport implements
        ViewResolver {

    private Map<String, ViewResolver> resolvers;
    private String DefaultViewResolverName = null;

    public Map<String, ViewResolver> getResolvers() {
        return resolvers;
    }

    public void setResolvers(Map<String, ViewResolver> resolvers) {
        this.resolvers = resolvers;
    }

    public String getDefaultViewResolverName() {
        return DefaultViewResolverName;
    }

    public void setDefaultViewResolverName(String defaultViewResolverName) {
        DefaultViewResolverName = defaultViewResolverName;
    }
    
    private ViewResolver getDefaultViewResolver() {
        return this.resolvers.get(this.DefaultViewResolverName);
    }

    @Override
    public View resolveViewName(String viewName, Locale locale)
            throws Exception {
        ViewResolver viewResolver = null;
        if (viewName.equalsIgnoreCase("json") || viewName.equalsIgnoreCase("xml")) {
            return this.resolvers.get(viewName).resolveViewName(viewName, locale);
        }
        int index = viewName.indexOf(":");
        if (index == -1) {
            viewResolver = this.getDefaultViewResolver();
        } else {
            if (viewName.substring(0, index).equalsIgnoreCase("redirect")) {
                viewResolver = this.getDefaultViewResolver();
            } else {
                viewResolver = this.resolvers.get(viewName.substring(0, index));
                viewName = viewName.substring(index + 1);
            }
        }
        
        if(viewResolver != null) {
            return viewResolver.resolveViewName(viewName, locale);
        }
        return null;
    }

}

 

3.创建MyBaseController, 使所有的Controller都对其继承

public abstract class MyBaseController {

    public final String JSON = "json";
    public final String XML = "xml";

    protected String result(ResultType resultType, String url) {

        resultType = resultType == null ? ResultType.JSP : resultType;

        String ext = RequestUtil.getLookupPathExtension(ThreadLocalUtil
                .getRequest());

        if (ext == null) {
            if (resultType.equals(ResultType.JSON)
                    || resultType.equals(ResultType.XML)) {
                return resultType.name().toLowerCase();
            }
        } else if ("JSON".equalsIgnoreCase(ext) || "XML".equalsIgnoreCase(ext)) {
            return ext;
        }
        if (resultType.equals(ResultType.REDIRECT)) {
            ext = RequestUtil.getLookupPathFullExtension(ThreadLocalUtil
                    .getRequest());
            if (ext != null) {
                url += ext;
            }
        }

        return resultType.name().toLowerCase() + ":" + url;
    }
}

 

 4.SpringMVC配置

    <bean name="viewResolver"
        class="org.matrixframework.common.viewresolver.MultipleViewResolver">
        <property name="defaultViewResolverName" value="jsp" />
        <property name="resolvers">
            <map>
                <entry key="jsp">
                    <!-- JSP视图解析器 -->
                    <bean name="internalResourceViewResolver"
                        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                        <property name="prefix" value="/WEB-INF/views/jsp" />
                        <property name="suffix" value=".jsp" />
                    </bean>
                </entry>
                <entry key="json">
                    <!-- JSON视图解析器 -->
                    <bean name="jsonViewResolver" class="com.framework.common.viewresolver.JsonViewResolver">
                        <property name="view">
                            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                                <property name="contentType" value="text/html; charset=utf-8"/>
                            </bean>
                        </property>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

 

posted @ 2015-06-29 21:53  liuxiujun  阅读(3700)  评论(0编辑  收藏  举报