实现通过请求url后缀判断返回值类型, 例如 localhost:8080/list 返回JSP页面, localhost:8080/list.json返回json格式数据
1.创建一枚举返回值类型
public enum ResultType { REDIRECT, JSP, JSON, XML }
2.创建自定义的viewResolver
1 public class JsonViewResolver implements ViewResolver { 2 3 private View view; 4 5 public View getView() { 6 return view; 7 } 8 9 public void setView(View view) { 10 this.view = view; 11 } 12 13 @Override 14 public View resolveViewName(String viewName, Locale locale) 15 throws Exception { 16 return this.view; 17 } 18 19 }
JsonViewResolver
1 public class XmlViewResolver implements ViewResolver { 2 3 private View view; 4 5 public View getView() { 6 return view; 7 } 8 9 public void setView(View view) { 10 this.view = view; 11 } 12 13 @Override 14 public View resolveViewName(String viewName, Locale locale) 15 throws Exception { 16 return this.view; 17 } 18 19 }
XmlViewResolver
1 public class MultipleViewResolver extends WebApplicationObjectSupport implements 2 ViewResolver { 3 4 private Map<String, ViewResolver> resolvers; 5 private String DefaultViewResolverName = null; 6 7 public Map<String, ViewResolver> getResolvers() { 8 return resolvers; 9 } 10 11 public void setResolvers(Map<String, ViewResolver> resolvers) { 12 this.resolvers = resolvers; 13 } 14 15 public String getDefaultViewResolverName() { 16 return DefaultViewResolverName; 17 } 18 19 public void setDefaultViewResolverName(String defaultViewResolverName) { 20 DefaultViewResolverName = defaultViewResolverName; 21 } 22 23 private ViewResolver getDefaultViewResolver() { 24 return this.resolvers.get(this.DefaultViewResolverName); 25 } 26 27 @Override 28 public View resolveViewName(String viewName, Locale locale) 29 throws Exception { 30 ViewResolver viewResolver = null; 31 if (viewName.equalsIgnoreCase("json") || viewName.equalsIgnoreCase("xml")) { 32 return this.resolvers.get(viewName).resolveViewName(viewName, locale); 33 } 34 int index = viewName.indexOf(":"); 35 if (index == -1) { 36 viewResolver = this.getDefaultViewResolver(); 37 } else { 38 if (viewName.substring(0, index).equalsIgnoreCase("redirect")) { 39 viewResolver = this.getDefaultViewResolver(); 40 } else { 41 viewResolver = this.resolvers.get(viewName.substring(0, index)); 42 viewName = viewName.substring(index + 1); 43 } 44 } 45 46 if(viewResolver != null) { 47 return viewResolver.resolveViewName(viewName, locale); 48 } 49 return null; 50 } 51 52 }
MultipleViewResolver
3.创建MyBaseController, 使所有的Controller都对其继承
1 public abstract class MyBaseController { 2 3 public final String JSON = "json"; 4 public final String XML = "xml"; 5 6 protected String result(ResultType resultType, String url) { 7 8 resultType = resultType == null ? ResultType.JSP : resultType; 9 10 String ext = RequestUtil.getLookupPathExtension(ThreadLocalUtil 11 .getRequest()); 12 13 if (ext == null) { 14 if (resultType.equals(ResultType.JSON) 15 || resultType.equals(ResultType.XML)) { 16 return resultType.name().toLowerCase(); 17 } 18 } else if ("JSON".equalsIgnoreCase(ext) || "XML".equalsIgnoreCase(ext)) { 19 return ext; 20 } 21 if (resultType.equals(ResultType.REDIRECT)) { 22 ext = RequestUtil.getLookupPathFullExtension(ThreadLocalUtil 23 .getRequest()); 24 if (ext != null) { 25 url += ext; 26 } 27 } 28 29 return resultType.name().toLowerCase() + ":" + url; 30 } 31 }
MyBaseController
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>
时间: 2024-10-04 09:25:24