• springmvc两种非注解的处理器适配器


    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>springmvc_01</display-name>
        
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
        
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    java源代码

    package com.zhou.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    public class AccountController implements Controller{
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("index.jsp");
            modelAndView.addObject("account", "122553");
            return modelAndView;
        }
    
    
    }
    package com.zhou.controller;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.HttpRequestHandler;
    
    public class UserController implements HttpRequestHandler{
    
    
        public String login(){
            return null;
        }
    
        @Override
        public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setAttribute("user", "zhangsan");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    
       
    }
    
    
    
    
    

    springmvc.xml

    <?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: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-3.2.xsd   
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">
        
        <!-- 配置处理器映射器 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        
        <!-- 配置处理器适配器 -->
        <!-- 以bean的name属性值作为url -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
        <!-- 另一个处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter">
            
        </bean>
        <!-- 以bean的id属性值作为url -->
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/user1.action">user</prop>
                    <prop key="/user2.action">user</prop>
                    <prop key="/account.action">account</prop>
                </props>
            </property>
        </bean>
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
        
        <!-- controller -->
        <bean id="user" name="/user.action" class="com.zhou.controller.UserController"></bean>
        
        <bean id="account" class="com.zhou.controller.AccountController"></bean>
    </beans>  
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter这个适配器是处理实现了

    org.springframework.web.servlet.mvc.Controller接口的handler,如下代码可以解释
    public boolean supports(Object handler) {
            return (handler instanceof Controller);
        }
    org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter这个适配器是处理实现了
    org.springframework.web.HttpRequestHandler接口的handler,如下代码可以解释

    因此非注解的handler要实现处理器适配器相对应需要的接口,才能被适配器匹配到。

  • 相关阅读:
    1860 最大数
    1164 统计数字
    1063 合并果子
    1098 均分纸牌
    2806 红与黑
    1168 火柴棒等式
    1910 递归函数
    2774 火烧赤壁
    2017.0705.《计算机组成原理》-存储器
    2017.0704.《计算机组成原理》-动态RAM
  • 原文地址:https://www.cnblogs.com/zhouquan-1992-04-06/p/6528690.html
Copyright © 2020-2023  润新知