一、注意点: 版本问题

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;
} }

【SpringMVC笔记】第四课 注解的处理器映射器和处理器适配器使用的更多相关文章

  1. springMVC入门(三)------springMVC的处理器映射器和处理器适配器配置

    简介 springMVC的处理器映射器和处理器适配器存在多种配置,因此在此专门做一个总结 常见处理器映射器.适配器的配置 springmvc多个映射器多个处理器可以并存 所有的映射器都实现了Handl ...

  2. spring_配置处理器对象、处理器映射器、处理器适配器、视图解析器

    创建spring配置文件:application-context.xml. 创建处理器类 package com.lanou.demo.controller;public class BookCont ...

  3. SpringMVC注解配置处理器映射器和处理器适配器

    一.springmvc.xml中配置方式 <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.me ...

  4. 【SpringMVC笔记】第三课 处理器映射器+处理器适配器

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

  5. SpringMVC学习记录二——非注解和注解的处理器映射器和适配器

    3      非注解的处理器映射器和适配器 3.1      非注解的处理器映射器 处理器映射器: org.springframework.web.servlet.handler.BeanNameUr ...

  6. 【springmvc笔记】第二课 环境搭建和第一个springmvc例子

    1. 开发工具准备 eclipse + jdk1.7 spring-framework-4.3.9.RELEASE 2. 新建Dynamic Web Project项目,命名为springmvc. 3 ...

  7. springmvc 源码分析(三) -- 自定义处理器映射器和自定义处理器适配器,以及自定义参数解析器 和错误跳转自定页面

    测试环境搭建: 本次搭建是基于springboot来实现的,代码在码云的链接:https://gitee.com/yangxioahui/thymeleaf.git DispatcherServlet ...

  8. 处理器映射器(HandlerMapping)及处理器适配器(HandlerAdapter)详解(二)

    注解的 处理器映射器 和 处理器适配器 介绍 注解的映射器: 在 Spring3.1 之前使用 DefaultAnnotationHandlerMapping 注解映射器(根据 DispatcherS ...

  9. springMVC非注解常用的"处理器映射器"、"适配器"、"处理器"

    非注解处理器映射器1. org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping url 到bean name的映射2. or ...

随机推荐

  1. hduoj------确定比赛名次

    拓扑排序结合代码的完整理解 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. Lucene的索引不跨平台

    在windows上使用Lucene生成索引文件,将索引文件复制到Linux服务器上,报错"校验错误,可能是硬件问题". 所以,Lucene的跨平台只是代码跨平台,生成的索引不跨平台 ...

  3. 常见Web前端开发笔试题

    1.什么是web标准? WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为 (Behavior). 对应的标准也分 ...

  4. PowerDesigner 的常用方法

    http://www.cnblogs.com/studyzy/archive/2008/01/23/1050194.html PowerDesigner 的常用方法 修改外键命名规则 选择Databa ...

  5. Android广播BroadcastReceiver

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  6. Google Guice之作用域

    默认情况下,Guice获取一个实例时.每次都会返回一个新的对象. 这个行为能够通过scopes进行配置.Scopes同意你复用实例: 应用整个生命周期(@Singleton),会话(@Session) ...

  7. Cocos2d-x游戏移植到WP8之路 -- c++和c#交互

    Cocos2d-x是眼下最流行的手机游戏引擎之中的一个,开源.轻量.多平台等的诸多特性使得它被非常多国内外手游开发人员所喜爱. 利用Cocos2d-x来开发Windows Phone 8的游戏相同也是 ...

  8. 办公技巧:局域网内设置固定ip

    第一步:查看自己现在的网络配置 打开命令行,输入:ipconfig /all 第二步:打开控制面板 - 网络配置 根据CMD命令的ipconfig信息对号入座填入即可. 然后,重启一下WIFI即可. ...

  9. unity 获得子节点

    transform.FindChild("childName") transform.FindChild("childName/grandChildName") ...

  10. texturePacker黄色文件夹和蓝色文件夹

    texturePacker中以黄色文件夹添进来的文件夹中的图片,不用带文件夹名,以蓝色文件夹添进来的文件要带文件夹名.