【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;
} }
【SpringMVC笔记】第四课 注解的处理器映射器和处理器适配器使用的更多相关文章
- springMVC入门(三)------springMVC的处理器映射器和处理器适配器配置
简介 springMVC的处理器映射器和处理器适配器存在多种配置,因此在此专门做一个总结 常见处理器映射器.适配器的配置 springmvc多个映射器多个处理器可以并存 所有的映射器都实现了Handl ...
- spring_配置处理器对象、处理器映射器、处理器适配器、视图解析器
创建spring配置文件:application-context.xml. 创建处理器类 package com.lanou.demo.controller;public class BookCont ...
- SpringMVC注解配置处理器映射器和处理器适配器
一.springmvc.xml中配置方式 <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.me ...
- 【SpringMVC笔记】第三课 处理器映射器+处理器适配器
第二课的例子中,在springmvc.xml中配置使用了第一种处理器映射器和处理器适配器,如下所示. <!-- 配置第一种处理器映射器 BeanNameUrlHandlerMapping --& ...
- SpringMVC学习记录二——非注解和注解的处理器映射器和适配器
3 非注解的处理器映射器和适配器 3.1 非注解的处理器映射器 处理器映射器: org.springframework.web.servlet.handler.BeanNameUr ...
- 【springmvc笔记】第二课 环境搭建和第一个springmvc例子
1. 开发工具准备 eclipse + jdk1.7 spring-framework-4.3.9.RELEASE 2. 新建Dynamic Web Project项目,命名为springmvc. 3 ...
- springmvc 源码分析(三) -- 自定义处理器映射器和自定义处理器适配器,以及自定义参数解析器 和错误跳转自定页面
测试环境搭建: 本次搭建是基于springboot来实现的,代码在码云的链接:https://gitee.com/yangxioahui/thymeleaf.git DispatcherServlet ...
- 处理器映射器(HandlerMapping)及处理器适配器(HandlerAdapter)详解(二)
注解的 处理器映射器 和 处理器适配器 介绍 注解的映射器: 在 Spring3.1 之前使用 DefaultAnnotationHandlerMapping 注解映射器(根据 DispatcherS ...
- springMVC非注解常用的"处理器映射器"、"适配器"、"处理器"
非注解处理器映射器1. org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping url 到bean name的映射2. or ...
随机推荐
- centos7安装MySQL5.7无法设置密码问题
前言 在使用centos7系统yum方式安装MySQL5.7后 不知道默认密码是多少 知道后没办法修改? 一.找到MySQL密码 service mysqld start vim /var/log/ ...
- idea中pom.xml关于oracle配置
由于Oracle授权问题,Maven3不提供Oracle JDBC driver,为了在Maven项目中应用Oracle JDBC driver,必须手动添加到本地仓库. Orace驱动的下载:htt ...
- 【LeetCode】52. N-Queens II
N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- Eclipse 选中变量高亮显示设置
- Remote 'attachhome' failed on nodes:XXX
RAC安装过程中,在安装GI的时候报如下错误: 解决方法: 根据提示执行以下脚本 $ /u01/app//grid/oui/bin/runInstaller -attachHome -noCluste ...
- golang(5)使用beego 开发 api server 和前端同学拆分开发,使用swagger
1,beego api Swagger 是一个规范和完整的框架,用于生成.描写叙述.调用和可视化 RESTful 风格的 Web 服务.整体目标是使client和文件系统作为服务器以相同的速度来更新. ...
- Python 列表 insert() 方法
描述 Python 列表 insert() 方法将指定对象插入到列表中的指定位置. 语法 insert() 方法语法: L.insert(index,obj) 参数 index -- 对象obj需要插 ...
- 【Android】5.5 状态切换(Switch)和评级条(RatingBar)
分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 1.利用Switch或者ToggleButton切换状态 如果只有两种状态,可以用ToggleButton控件或S ...
- RednaxelaFX写的文章/回答的导航帖
https://www.zhihu.com/people/rednaxelafx/answers http://hllvm.group.iteye.com/group/topic/44381#post ...
- Storm API,Tutorial,Trident,Windowing BOOKS
http://storm.apache.org/releases/1.0.0/Trident-API-Overview.html https://stackoverflow.com/questions ...