一、注意点: 版本问题

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. iOS GCD中级篇 - dispatch_group的理解及使用

    上一篇GCD基础篇,以及同步.异步,并发.并行几个概率的理解 关于dispatch_group的概念以及几种场景下的使用 1.关于dispatch_group 把一组任务提交到队列中,这些队列可以不相 ...

  2. HDUOJ---The number of divisors(约数) about Humble Numbers

    The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  3. 个人网站不输入www.直接domain.com访问不了,输入www.domain.com能访问

      网站访问跳转到/cgi-sys/defaultwebpage.cgi页面原因之一ip地址不对解决后,www.domain.com是可以访问了.但是直接domain.com去不能?   我记得刚开始 ...

  4. Linux命令-定时任务命令:crontab

    linux定时任务命令相当于windows=>附件=>系统工具=>计划任务程序. 要想让linux定时任务生效,首先应该先启动crond服务,并且给这个服务设置开机自自动. 默认li ...

  5. Oracle子查询相关内容(包含TOP-N查询和分页查询)

    本节介绍Oracle子查询的相关内容: 实例用到的数据为oracle中scott用户下的emp员工表,dept部门表,数据如下: 一.子查询 1.概念:嵌入在一个查询中的另一个查询语句,也就是说一个查 ...

  6. UNIX域套接字编程和socketpair 函数

    一.UNIX Domain Socket IPC socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络soc ...

  7. 项目打成jar包

    distributionManagement 为发布到本地参考的地址 repository 设置从本地maven库拉取jar包 <project xmlns:xsi="http://w ...

  8. mac eclipse 执行adb 命令,提示command not fount

    java执行命令: p = Runtime.getRuntime().exec(commandStr); [INFO ] 执行命令结果:nullbash: adb: command not found ...

  9. 使用R语言分析股价波动

    今天看的R语言.做个笔记. 使用R语言读取雅虎財经数据.分析微软公司(股票代码:MSFT)在2015年股价波动超过百分之十的日期. 然后通过检索新闻的方式,看看微软当天有什么新闻发生,导致股价波动. ...

  10. cocos2d-x开发记录:一,搭建环境

    首先我下载的cocos2D版本为2.1.2版本,我们安装好VS后,新建一个解决方案,这里很重要,假设我的cocos解压后的路径为E:\cocos2d-2.1rc0-x-2.1.2-hotfix\coc ...