【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 ...
随机推荐
- 给maven仓库添加镜像
用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...
- Wunderlist 云端任务管理(Todo list)工具
Wunderlist 是一个云端任务管理(Todo list)工具,支持 iPhone, iPad, Android, Windows, Mac OSX 以及 Web 端轻松同步,实现了真正意义上的跨 ...
- 跨域在嵌入页面iframe中设置cookie
在IIS HTTP响应头 中 添加: 名称:p3p 值:CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"
- python练习笔记——map | sum | pow 的应用
1 函数简要 map 函数 | sum 函数 | pow函数 | lambda函数 2 简要计算 2.1 1^2 + 2^2 + 3^2 .....9^2 方法1 print([pow(x,2 ...
- JMeter UI 启动时报错
Problem After running jmeter.bat command under Windows 7 with cmd the following error is produced:W ...
- IIS下浏览指定文件(如:web.config)
具体步骤如下: 1.快捷键:Ctrl + R 2.输入:%windir%\System32\inetsrv\config\applicationHost.config 3.注销:fileExtensi ...
- [Android&Java]浅谈设计模式-代码篇:观察者模式Observer
观察者,就如同一个人,对非常多东西都感兴趣,就好像音乐.电子产品.Game.股票等,这些东西的变化都能引起爱好者们的注意并时刻关注他们.在代码中.我们也有这种一种方式来设计一些好玩的思想来.今天就写个 ...
- Python expandtabs() 方法
描述 expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8. 从头开始数,数到第一个\t正好为8个空格,不足则补空格,如果还有\t, ...
- laravel 如何引入自己的函数或类库
例如在app下建一个Common文件夹 在Common下建一个function.php 放入公共函数 例如: function test(){ echo 'this is a test'; } 在项目 ...
- Java:集合,Collections工具类用法
Collections工具类提供了大量针对Collection/Map的操作,总体可分为四类,都为静态(static)方法: 1. 排序操作(主要针对List接口相关) reverse(List li ...