非注解的处理器映射器和适配器
非注解的处理器映射器
-
之前的入门Demo中使用的就是非注解的处理器映射器:
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping -
另一种非注解的处理器映射器:
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
进行这个配置之后,访问
http://localhost:8080/SpringMVCFirst/queryItems1.action
和
http://localhost:8080/SpringMVCFirst/queryItems2.action
都会进入商品列表的jsp页面。
非注解的处理器适配器
- 之前的入门Demo中使用的就是非注解的处理器适配器:
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,
查看该适配器的源码:
可以发现,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean通过此适配器进行适配、执行才可以。 - 另一种处理器适配器:
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,
查看该适配器的源码:
可以发现,所有实现了org.springframework.web.HttpRequestHandler 接口的Bean通过此适配器进行适配、执行才可以。
这种处理器适配器的Handler实现如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
从上边可以看出此适配器的handleRequest方法类似于原始的Servlet,通过这种方式可以设置response响应内容,比如返回json数据:
- 1
- 2
- 3
- 1
- 2
- 3
DispatcherSerlvet.properties
如果不配置springmvc.xml文件,应用程序照样可以正常运行。原因是:
在web.xml中配置的前端控制器org.springframework.web.servlet.DispatcherServlet的包中存在DispatcherSerlvet.properties。
前端控制器从上边的文件中加载处理映射器、适配器、视图解析器等组件,如果不在springmvc.xml中配置,使用DispatcherSerlvet.properties文件中配置的默认加载的。
注解的处理器映射器和适配器
在spring3.1之前和spring3.1之后的版本中使用的注解方式的处理器映射器和处理器适配器有所不同:
-
注解方式使用的处理器映射器:
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。 -
注解方式使用的处理器适配器:
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。
第一步:配置注解的映射器和适配器
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
注意:
springmvc使用<mvc:annotation-driven>
自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置文件中使用<mvc:annotation-driven>
替代上面配置的注解处理器和适配器的配置。
<mvc:annotation-driven>
默认还加载很多的参数绑定方法,比如json转换解析器就默认加载了。实际开发中我们一般使用<mvc:annotation-driven>
。
第二步:开发Handler(使用注解的方式)
使用@Controller注解标识它是一个控制器。也就相当于之前没有使用注解开发的Hander中的:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
第三步:配置Handler
第一种方式:
可以使用与原来的配置Handler的方式一样,一个一个的在springmvc.xml中配置Handler。
- 1
- 2
- 1
- 2
但是这种方式存在一个问题:如果开发了很多个Handler,那么就要挨个在springmvc.xml中进行配置,非常的繁琐。下面这种方式解决了这个问题。
第二种方式:
组件扫描器可以扫描controller、service……
使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan/>
自动扫描标记@controller的控制器类,配置如下:
- 1
- 2
- 1
- 2
第四步:部署访问