【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 ...
随机推荐
- SDUT 2608 Alice and Bob (巧妙的二进制)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...
- Google C++单元测试框架
一.概述 Google C++单元测试框架(简称Gtest),可在多个平台上使用(包括Linux, Mac OS X, Windows, Cygwin和Symbian),它提供了丰富的断言.致命和非致 ...
- 快速掌握activity的生命周期
activity的生命周期不管是在面试还是在工作中我们都会经常遇到,这当然也是非常基础的,基础也很重要哦,学会activity的生命周期对我们以后开发更健壮的程序会有很大帮助.下面来看一下Activi ...
- [原创] Java JDBC连接数据库,反射创建实体类对象并赋值数据库行记录(支持存储过程)
1.SqlHelper.java import java.lang.reflect.*; import java.sql.*; import java.util.*; public class Sql ...
- SQL SERVER 2005允许自定义聚合函数-表中字符串分组连接
不多说了,说明后面是完整的代码,用来将字符串型的字段的各行的值拼成一个大字符串,也就是通常所说的Concat 例如有如下表dict ID NAME CATEGORY 1 RED COLOR ...
- JMeter学习笔记---作用域规则
JMeter测试树中既包含遵循分层规则的测试元件(监听器.配置元件.后置处理器.前置处理器.断言.定时器),又包含遵循顺序规则的测试元件(逻辑控制器.采样器),测试人员创建测试计划的同时,实际上就创建 ...
- 摘:VC开发数据库基础之ADO篇
一.ADO简介ADO(ActiveX Data Object)是Microsoft数据库应用程序开发的新接口,是建立在OLE DB之上的高层数据库访问技术,请不必为此担心,即使你对OLE DB,COM ...
- Linux内核同步 - RCU基础
一.前言 关于RCU的文档包括两份,一份讲基本的原理(也就是本文了),一份讲linux kernel中的实现.第二章描述了为何有RCU这种同步机制,特别是在cpu core数目不断递增的今天,一个性能 ...
- centos部署supervisor
#!/bin/bash yum -y install python-setuptools ping pypi.python.org -c 4 >/dev/null 2>&1 eas ...
- Python 字典 update() 方法
描述 Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中. 用法与 Python dict() 函数相似. 语法 update ...