关闭

自定义MultipleViewResolver

标签: objectstringexceptionclassnull
536人阅读 评论(0) 收藏 举报
分类:

1.MultipleViewResolver.java

import java.util.Locale;
import java.util.Map;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

public class MultipleViewResolver implements ViewResolver {
 
 private Map<String, ViewResolver> resolvers;

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

 public View resolveViewName(String viewName, Locale locale)
   throws Exception {
  int index = viewName.indexOf('.');//first '.'
  ViewResolver resolver = null;
  
  if (index > -1) {
   int p = viewName.indexOf("?");//first '?'
   p = p == -1 ? viewName.length() : p;
   if (resolvers.containsKey(viewName.substring(index + 1, p))) {
    resolver = resolvers.get(viewName.substring(index+1, p));
   } else {
    resolver = resolvers.get(viewName.substring(index+1));
   }
  } else {
   resolver = resolvers.get(viewName);
  }

  if (resolver != null) {
   return resolver.resolveViewName(viewName, locale);
  }
  
  return null;
 }

 

2.jsonView.java

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kframe.util.JSONResponseUtil;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.view.AbstractView;

public class JsonView extends AbstractView implements Ordered {

 private int order;

 public JsonView() {
  setContentType("application/json;charset=UTF-8");
 }

 @SuppressWarnings("unchecked")
 @Override
 protected void renderMergedOutputModel(Map map, HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  Object o = map.get(JsonModelAndView.EXCLUDE_PROPERTY);
  if (o != null) {
   JSONResponseUtil.print(response, map .get(JsonModelAndView.MODEL_KEY), (String[]) o);
  } else {
   JSONResponseUtil.print(response, map .get(JsonModelAndView.MODEL_KEY));
  }
 }

 public void setOrder(int order) {
  this.order = order;
 }

 public int getOrder() {
  return order;
 }

}

 

3.JsonModelAndView.java

import org.springframework.web.servlet.ModelAndView;

public class JsonModelAndView extends ModelAndView {

 public static final String MODEL_KEY = "MODEL_KEY";
 
 public static final String EXCLUDE_PROPERTY = "EXCLUDE_PROPERTY";

 private static final String VIEW_NAME = "json";


 public JsonModelAndView() {
  setViewName(VIEW_NAME);
 }

 public JsonModelAndView(Object object) {
  addObject(MODEL_KEY, object);
  setViewName(VIEW_NAME);
 }
 
 public JsonModelAndView(Object object, String[] excludeProperty) {
  addObject(MODEL_KEY, object);
  addObject(EXCLUDE_PROPERTY, excludeProperty);
  setViewName(VIEW_NAME);
 }

 public void setModel(Object object) {
  addObject(MODEL_KEY, object);
 }

 public void setExcludeProperty(String[] excludeProperty) {
  addObject(EXCLUDE_PROPERTY, excludeProperty);
 }
}

 

4.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2339504次
    • 积分:31348
    • 等级:
    • 排名:第156名
    • 原创:962篇
    • 转载:826篇
    • 译文:6篇
    • 评论:39条
    文章分类
    最新评论
    我的栏目
    我的栏目测试