SpringMVC--适配器与映射器(二)

一、处理器适配器

  处理器适配器 HandlerAdapter:作用是根据映射器找到的处理器 Handler 信息,按照特定的规则去执行相关的处理器 Handler。
  其配置方式有两种,一种是基于 xml 的资源配置,也就是非注解的配置方式。另外一种就是基于 Annotation 注解的配置。其注解在代码中做上特殊标记,这些标记就可以编译、类加载、运行时被读取,然后去执行相应的处理。
  HandlerAdapter会根据适配器接口对后端控制器进行包装(适配),包装后即可对处理器进行执行,通过扩展处理器适配器可以执行多种类型的处理器,这里使用了适配器设计模式。

  1. 单控制器处理适配器
      它支持所有实现了 Controller 接口的 Handler 控制器,如果开发中编写了实现 Controller 接口的控制器,则SimpleControllerHandlerAdapter 适配器就会去执行 Controller 的具体方法。
      SimpleControllerHandlerAdapter简单控制器处理器适配器,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean通过此适配器进行适配、执行。
    配置:
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

控制器:

public class UserController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        User user=new User();
        user.setUsername("张三");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("/user/user.jsp");
        return modelAndView;
    }
}
  1. http 请求处理器适配器
      它要求编写的 Handler 需要实现 HttpRequestHandler 接口。使用这种 Handler 的开发方式,方便开发者获取 request 的相关 http 请求信息,以及设置返回对象 response 的一些参数。
      HttpRequestHandlerAdapter,http请求处理器适配器,所有实现了org.springframework.web.HttpRequestHandler 接口的Bean通过此适配器进行适配、执行。
    配置:
 <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

控制器:

// 必须配置:HttpRequestHandlerAdapter 适配器
public class UserController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        user.setUsername("李四");
        // 填充数据
        request.setAttribute("user", user);
        // 视图
        request.getRequestDispatcher("/user/user.jsp").forward(request, response);
    }
}

从上边可以看出此适配器器的handleRequest方法没有返回ModelAndView,可通过response修改定义响应内容,比如返回json数据:

response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");

二、映射适配器

最常用的两个映射处理器分别为BeanNameUrlHandlerMapping和SimpleUrlHandlerMapping。
如果没有明确声明任何处理器映射,spring会默认使用BeanNameUrlHandlerMapping,但如果明确声明了其它的处理器映射,则需要将BeanNameUrlHandlerMapping明确声明出来,而且在每个包含被映射的bean的配置文件中都要加入。BeanNameUrlHandlerMapping

  1. BeanName 映射器
    BeanNameUrlHandlerMapping只会处理那些前缀为"/"的urls。
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- 映射器 -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--使用此bean名作为请求路径
    注意:bean必须以/,必须以.do或其它.xx作后缀。
    -->
    <bean id="/user.action" class="com.neuedu.controller.UserController"/>

使用BeanNameUrlHandlerMapping映射器有两点明显不足:
处理器Bean的id为一个url请求路径,而不是Bean的名称,有些不伦不类。
处理器Bean的定义与请求url绑定在了一起。若出现多个url请求同一个处理器的情况,就需要在Spring容器中配置多个该处理器类的<bean>。这将导致容器会创建多个该处理器实例。
访问地址:
http://localhost:9000/user.action

  1. 简单映射器
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
    <!-- 映射器 -->
    <!-- controller配置 -->
    <bean id="user" name="user" class="com.neuedu.controller.UserController"/>
    <!--简单url映射  -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!-- 对itemsController1进行url映射,url是/user1.action -->
                <prop key="/user1.action">user</prop>
                <prop key="/user2.action">user</prop>
            </props>
        </property>
    </bean>

访问地址:
http://localhost:9000/user2.action
http://localhost:9000/user1.action

三、注解适配器

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

注解式处理器适配器,对标记@ResquestMapping的方法进行适配。
从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。

  1. 组件扫描器
    使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan自动扫描标记@controller的控制器类。
<!-- 注件扫描器 -->
<context:component-scan base-package="com.neuedu.controller"/>
  1. <mvc:annotation-driven>
    springmvc使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。

  2. 处理器与适配器配置

<!-- 映射处理器 -->
<bean   class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- 映射适配器 -->
<bean   class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<!--组件扫描器 -->
<context:component-scan base-package="com.neuedu.controller"></context:component-scan>
或:(推荐使用以下方式)
<!—组件扫描器 -->
<context:component-scan base-package="com.neuedu.controller"></context:component-scan>
<!-- 自动加载处理器与适配器 -->
<mvc:annotation-driven/>

注意:
如果使用注解解析器,就不要再添加其它的适配器与控制器。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容