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; } }
  • 相关阅读:
    struts的action直接输出中文备注【原创】
    iframe高度处理【原创】
    Gel备注【原创】
    关于用jsp生成xml的问题【原创】
    网上流行的flash切换图片之研究【原创】
    c#连mysql的latin1编码乱码问题
    windows phone 7图片搜索小工具(源码)
    家猫js类库
    理解c#中的readonly,const与enum枚举
    Silverlight 雷达图和一种特殊泡泡画法
  • 原文地址:https://www.cnblogs.com/30go/p/7226754.html
Copyright © 2011-2022 走看看