New Support Classes for @RequestMapping methods in Spring MVC 3.1
Spring 3.1 introduced a new set of support classes for @RequestMapping
methods called RequestMappingHandlerMapping
and RequestMappingHandlerAdapter
respectively. They are recommended for use and even required to take advantage of new features in Spring MVC 3.1 and going forward. The new support classes are enabled by default by the MVC namespace and the MVC Java config but must be configured explicitly if using neither. This section describes a few important differences between the old and the new support classes.
Prior to Spring 3.1, type and method-level request mappings were examined in two separate stages — a controller was selected first by the DefaultAnnotationHandlerMapping
and the actual method to invoke was narrowed down second by the AnnotationMethodHandlerAdapter
.
With the new support classes in Spring 3.1, the RequestMappingHandlerMapping
is the only place where a decision is made about which method should process the request. Think of controller methods as a collection of unique endpoints with mappings for each method derived from type and method-level @RequestMapping
information.
This enables some new possibilities. For once a HandlerInterceptor
or a HandlerExceptionResolver
can now expect the Object-based handler to be a HandlerMethod
, which allows them to examine the exact method, its parameters and associated annotations. The processing for a URL no longer needs to be split across different controllers.
There are also several things no longer possible:
- Select a controller first with a
SimpleUrlHandlerMapping
orBeanNameUrlHandlerMapping
and then narrow the method based on@RequestMapping
annotations. - Rely on method names as a fall-back mechanism to disambiguate between two
@RequestMapping
methods that don’t have an explicit path mapping URL path but otherwise match equally, e.g. by HTTP method. In the new support classes@RequestMapping
methods have to be mapped uniquely. - Have a single default method (without an explicit path mapping) with which requests are processed if no other controller method matches more concretely. In the new support classes if a matching method is not found a 404 error is raised.
The above features are still supported with the existing support classes. However to take advantage of new Spring MVC 3.1 features you’ll need to use the new support classes.
---------------------------------------------------------------------------------------
翻译一下这段官方的文档:
----------------------------------------------------------------------------------------
Spring MVC 3.1 中用一些新类支持注解符号@RequestMapping
Spring3.1 介绍了一套新的类用于实现@RequestMapping 方法,它们分别是RequestMappingHandlerMapping和RequestMappingHandlerAdapter。官方建议开发者使用这套新的类,而且这也是spring mvc 3.1 新功能所必需的。默认这套新类功能是打开的,可以通过MVC命名空间和MVC java 配置文件来使用。如果不需要,则必须明确的注明。这一小节讲了新旧两套类的几个重要的区别。
spring3.1之前, 类型和方法级请求要被两个类分别处理: 一是DefaultAnnotationHandlerMapping类选择的一个控制器类,二是调用AnnotationMethodHandlerAdapter类去触发实际的方法。
spring3.1中, 类RequestMappingHandlerMapping是唯一处理这种请求的类。这个类决定哪个方法去处理这个请求。不妨把控制器方法看做是处理这类请求的唯一终点的方法的集合。
这样就会产生一些新的功能。就这一次HandlerInterceptor或者HandlerExceptionResolver类就能预计到基于对象的处理器成为一个HandlerMethod类,进而让他们保证正确的方法、参数和相关的注解。对URL的处理不再需要根据不同的控制器拆分。
下面是新的spring MVC中不再适用的:
1. 首先用SimpleUrlHandlerMappng或BeanNameUrlHandlerMapping选择一个控制器类,然后根据@RequestMapping注解选择一个方法。
2. 依靠方法名区分两个@RequestMapping方法的低效运行机制。@RequestMapping方法中没有显示地路径映射到URL路径但是在其他方面例如HTTP方法却是相同的。在新的spring MVC3.1中@Request方法无疑是显示唯一的映射的。
3.如果没有其他控制器方法处理的话,会有一个单独默认的方法(没有显示的路径映射)用来处理请求。然而在spring MVC3.1中如果一个想对应的方法没有找到,会报404错误。
上面这些功能在新的版本中仍然支持。但是如果要用spring MVC 3.1的新功能的话,还是要用新的这套类。
相关推荐
Spring MVC--2.@RequestMapping 映射请求
前段时间项目中用到了REST风格来开发程序,但是当用POST、PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, 而且服务器端通过request....
SpringMVC@RequestMapping(重点)@RequestParam@PathVariable示例
根据读@requestmapping源码,理解实现原理后,自己写的简单的仿@requestmapping的代码,将该代码放置到自己的工程中就可以用。
springmvc之@RequestMapping的简单demo
SpringMybatis项目基于@RequestMapping和RequstAttribute实现登录注册
2.Spring MVC @RequestMapping 注解案例 3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向...
springmvc关于@requestMapping和@requestParam的使用,详细代码,独立可用无需修改。
02@RequestMapping映射.md
5.2 @RequestMapping通配符和占位符 1 @RequestMapping注解 类上指定的URL相对于应用根目录 @RequestMapping注解可以使用在控制器类和控制器类的方法上,一般情况下,类定义处的@ RequestMapping提供初步的请求映射...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
弹簧 @Controller @RequestMapping @RequestParam,模型和ModelAndView Spring MVC实践
本篇文章主要介绍了Spring MVC之@RequestMapping 详解,RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。有兴趣的可以了解一下。
@RequestMapping注解可以使用请求URL、请求方式、请求参数、请求头和带占位符的URL等方式来映射请求;且包括REST与HiddenHttpMethodFilter相关内容
Spring mvc中@RequestMapping 6个基本用法小结
基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")
工程文件 博文链接:https://starscream.iteye.com/blog/1063966