场景:
SpringMVC Controller类中不同的方法对应不同的Rest请求地址和逻辑视图返回地址,但是为了模块化,各个方法的Rest地址前缀和返回逻辑视图的地址前缀都应该相同。其中请求Rest地址的前缀可以在类上用@RequestMapping声明,而返回视图的逻辑地址前缀仍需要手动写。本文通过使用Spring MVC拦截器为Controller类方法返回的逻辑视图动态添加了模块Rest地址前缀,因此,在Controller类的方法只需要返回逻辑视图名称。
拦截器代码:
package com; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class ViewInterceptor extends HandlerInterceptorAdapter { //Controller类名称 <-> Controller类映射地址 private Map<String, String> ControllerRestPathMap = new HashMap<String, String>() ; /** * 拦截返回的逻辑视图,并在逻辑视图上加上Controller映射地址 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { String requestRest = (String)request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE) ; //获取spring web容器 WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext()) ; //获取声明类在spring容器中的名称 String[] beanNames = webApplicationContext.getBeanNamesForType(((HandlerMethod) handler).getMethod().getDeclaringClass()) ; for (String beanName : beanNames) { if(!StringUtils.hasLength(beanName)){ continue ; } //映射当前类的Rest地址 if(ControllerRestPathMap.get(beanName)==null){ //获取类的映射Rest地址 RequestMapping requestMapping = webApplicationContext.findAnnotationOnBean(beanName, RequestMapping.class) ; if(requestMapping!=null&&!ObjectUtils.isEmpty(requestMapping.value())){ String basePath = requestMapping.value()[0] ; //去掉类请求地址后缀“/” if(basePath.lastIndexOf("/")==basePath.length()-1){ basePath = basePath.substring(0, basePath.length()-1) ; } ControllerRestPathMap.put(beanName, basePath) ; } } //根据请求Rest地址前缀匹配处理的类 if(ControllerRestPathMap.get(beanName)!=null&&requestRest.startsWith(ControllerRestPathMap.get(beanName))){ modelAndView.setViewName(ControllerRestPathMap.get(beanName)+"/"+modelAndView.getViewName()); break ; } } // System.out.println("映射地址 -> "+modelAndView.getViewName()); } }
注:
1.Controller类中每个方法对应的Rest地址,以属性的型式存放在Request中。可以通过如下方式获取: request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)
2.拦截Rest地址匹配策略:根据Rest地址的匹配方式,查找处理它的Controller类。找到Controller类之后,将Controller类的逻辑视图地址加到方法返回逻辑地址前面,构成完整的逻辑视图地址。
在Spring MVC配置文件中装载拦截器:
<mvc:interceptors> <bean class="com.ViewInterceptor"></bean> </mvc:interceptors>
相关推荐
springmvc调用业务逻辑
SpringMVC自定义多视图,多视图实现
spring 3.0 应用springmvc 构造RESTful
SpringMVC 使用JSON、XML视图,json-lib jackson,xstream castor,jibx jaxb2
我博客《T4模版生成SpringMVC构造REST代码》的源代码
springMVC 多视图配置(Jsp Freemarket Json)实例
SpringMVC 传递 和 返回对象实例 以及 zTree例子 SpringMVC 传递 和 返回对象实例 以及 zTree例子
springmvc 3.1 多视图,freemarker jsp
NULL 博文链接:https://carvin.iteye.com/blog/786817
主要是工作中遇到的一些springMVC前台往后台传递参数的一些不同形式的后台的解决方式
字符串、对象、List集合、Map集合
springmvc返回json的全局异常统一处理demo,有问题可以留言。
SSM笔记-SpringMVC自定义视图及重定向视图、语言国际化
springMVC返回json数据需要的两个架包,加入springMVC使用@ResponseBody才不会报错。
主要介绍了SpringMVC后端返回数据到前端代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springmvc-demo08-返回JSON数据.zip
理解视图和视图解析器的解析流程、使用常用的InternalResourceView和JstlView、实现自定义视图、实现重定向操作
SpringMVC及视图解析器
Spring MVC--5.视图和视图解析器
springmvc返回json用到的jar包