zoukankan      html  css  js  c++  java
  • 【SpringMVC笔记】第三课 处理器映射器+处理器适配器

    第二课的例子中,在springmvc.xml中配置使用了第一种处理器映射器和处理器适配器,如下所示。

            <!-- 配置第一种处理器映射器 BeanNameUrlHandlerMapping -->
            <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    
            <!-- 配置第一种处理器适配器 SimpleControllerHandlerAdapter -->
            <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

    接下来是第二种处理器映射器和处理器适配器。

            <!-- 配置第二种处理器映射器 SimpleUrlHandlerMapping -->
            <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                <property name="mappings">
                    <props>
                        <prop key="/getList2.action">userController</prop>
                        <prop key="/getList3.action">userController</prop>
                        <prop key="/getList4.action">user2Controller</prop>
                    </props>
                </property>
            </bean>
     
             <!-- 配置第二种处理器适配器 SimpleControllerHandlerAdapter -->
            <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

    完整的springmvc.xml 如下:

    注意点: springmvc允许多个映射器和多个适配器共存,并不会相互影响,我们在使用时,只需要使用一种即可。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.3.xsd        
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.3.xsd        
            ">
            
            <!-- 配置handler处理器 -->
            <bean id="userController" name="/getList.action" class="com.king.controller.UserController"></bean>
            <bean id="user2Controller" class="com.king.controller.User2Controller"></bean>
            
            <!-- 配置第一种处理器映射器 BeanNameUrlHandlerMapping -->
            <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
            
            <!-- 配置第二种处理器映射器 SimpleUrlHandlerMapping -->
            <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                <property name="mappings">
                    <props>
                        <prop key="/getList2.action">userController</prop>
                        <prop key="/getList3.action">userController</prop>
                        <prop key="/getList4.action">user2Controller</prop>
                    </props>
                </property>
            </bean>
            
            <!-- 配置第一种处理器适配器 SimpleControllerHandlerAdapter -->
            <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
     
             <!-- 配置第二种处理器适配器 SimpleControllerHandlerAdapter -->
            <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
            
             <!-- 配置视图解析器 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
            
    </beans>

    同时,由于适配器不同,对应的Controller实现的接口也不一样,第二种的User2Controller定义如下:

    这里返回到jsp页面有点类似于struts2。

    package com.king.controller;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.HttpRequestHandler;
    import com.king.pojo.User;
    
    public class User2Controller implements HttpRequestHandler{
    
        @Override
        public void handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            
            System.out.println("start User2Controller");
            
            List<User> list = new ArrayList<>(Arrays.asList(
                new User(1,"zhangsan",20),
                new User(1,"zhang2",22),
                new User(1,"zhang3",23),
                new User(1,"zhang4s",24)
            ));
            
            request.setAttribute("list", list);
            request.getRequestDispatcher("page/list.jsp").forward(request, response);
            
        }
    }

     运行tomcat后,在url中输入 getList.action,getList2.action,getList3.actioin,getList4.action都可以执行查询结果。

    其中  getList.action ->   第一种映射器、第一种适配器

             getList2.action + getList3.action ->   第二种映射器、第一种适配器

             getList4.action ->   第二种映射器、第二种适配器

  • 相关阅读:
    python学习日记(OOP访问限制)
    python学习日记(OOP——@property)
    python学习日记(OOP——静态方法和类方法)
    python学习日记(isinstance和issubclass)
    python学习日记(OOP——反射)
    python学习日记(初识面向对象)
    python学习日记(内置、匿名函数练习题)
    python学习日记(内置函数)
    python学习日记(内置函数补充)
    Raft一致性协议
  • 原文地址:https://www.cnblogs.com/30go/p/7226646.html
Copyright © 2011-2022 走看看