zoukankan      html  css  js  c++  java
  • 【SpringMVC笔记】第四课 注解的处理器映射器和处理器适配器使用

    一、注意点: 版本问题

    spring3.2以前的版本,注解的映射器和适配器使用以下两个类.

    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping.class
    
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.class

    在新版本的源码中可以看到以下注释:

    在3.2 包含及以后的版本中使用如下类:

    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.class
    
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.class

    二、使用注解和非注解的差别

     1.  使用非注解时,在controller中只能实现一个方法,方法名和参数固定,不能使用多个方法。

    2.   使用注解时,允许自定义任意方法。

    3.  使用注解时,映射器和适配器需要同时使用,不可注解与非注解混搭使用。

     三、注解的简写方式

    如下,提供了更简便的注解配置方法,以后生产环境中可直接使用该注解配置。

    <!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
    <!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
            <!-- 
                           简化后的注解处理器映射器和适配器 :不仅包含上面映射器和适配器,还加载了很多参数绑定方法
             -->
            <mvc:annotation-driven></mvc:annotation-driven>

    四、开发过程

      1.   修改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: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        
            ">
            
    <!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
    <!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
            <!-- 
                           简化后的注解处理器映射器和适配器 :不仅包含上面映射器和适配器,还加载了很多参数绑定方法
             -->
            <mvc:annotation-driven></mvc:annotation-driven>
            
            <!-- 配置handler处理器 -->
            <bean class="com.king.controller.UserController"></bean>
            
             <!-- 配置视图解析器 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
    
    </beans>

     2. 修改UserController.java类如下:

    package com.king.controller;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.king.pojo.User;
    
    @Controller
    public class UserController { @RequestMapping("/getList") public ModelAndView getList(){ System.out.println("start UserController"); 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) )); ModelAndView mv = new ModelAndView(); mv.addObject("list", list); mv.setViewName("page/list.jsp"); return mv; } }
  • 相关阅读:
    PAT 解题报告 1009. Product of Polynomials (25)
    PAT 解题报告 1007. Maximum Subsequence Sum (25)
    PAT 解题报告 1003. Emergency (25)
    PAT 解题报告 1004. Counting Leaves (30)
    【转】DataSource高级应用
    tomcat下jndi配置
    java中DriverManager跟DataSource获取getConnection有什么不同?
    理解JDBC和JNDI
    JDBC
    Dive into python 实例学python (2) —— 自省,apihelper
  • 原文地址:https://www.cnblogs.com/30go/p/7226754.html
Copyright © 2011-2022 走看看