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.