`
rjw
  • 浏览: 56688 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

New Support Classes for @RequestMapping methods in Spring MVC 3.1

阅读更多

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 or BeanNameUrlHandlerMapping 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的新功能的话,还是要用新的这套类。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics