一、使用注解的处理器映射器和适配器

1)
在这个下面的/org/springframework/web/servlet/DispatcherServlet.properties文件,从这个文件中加载处理器映射器、适配器、视图解析器等组件,如果不在springmvc.xml中配置,使用默认加载的。其中:
  默认加载的注解映射器:
在spring3.1之前使用---org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
在spring3.1之后使用---org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
  默认加载的注解适配器
在spring3.1之前使用---org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
在spring3.1之后使用---org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
 
配置注解的适配器和映射器:
注解的映射器和注解的适配器必须配对使用
ItemsController3是Hander,交给spring初始化bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <bean class="com.cy.controller.ItemsController3"></bean> <!--注解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 视图解析器
解析jsp视图,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>

开发Handler:

//使用Controller标识 它是一个控制器
@Controller
public class ItemsController3{ //商品查询列表
//@RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url
//一般建议将url和方法写成一样
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {
//调用service查找 数据库,查询商品列表,这里使用静态数据模拟
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!"); Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!"); itemsList.add(items_1);itemsList.add(items_2); //返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当 于request的setAttribut,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsList); //指定视图
modelAndView.setViewName("/WEB-INF/jsp/itemsList.jsp");
return modelAndView;
}
}

浏览器中访问:http://localhost:8080/springMVC/queryItems.action能看到列表;

2)上面的程序中使用自动扫描:

<!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 -->
<!-- 可以扫描controller、service、...这里让扫描controller,指定controller的包-->
<context:component-scan base-package="com.cy.controller"></context:component-scan> <!--注解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 视图解析器
解析jsp视图,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

依然能访问;

3)上面的程序使用 注解驱动配置: 代替上面的注解映射器和注解适配器:

<!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描 -->
<!-- 可以扫描controller、service、...这里让扫描controller,指定controller的包-->
<context:component-scan base-package="com.cy.controller"></context:component-scan> <!-- mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,实际开发时使用mvc:annotation-driven-->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 视图解析器
解析jsp视图,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

http://localhost:8080/springMVC/queryItems.action   访问ok

 4)上面的视图解析器配置视图名的前缀、后缀:
<!-- 视图解析器
解析jsp视图,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>

controller中view的返回就这样:

//指定视图
modelAndView.setViewName("itemsList");
return modelAndView;

springMVC学习(2)的更多相关文章

  1. SpringMVC学习系列-后记 解决GET请求时中文乱码的问题

    SpringMVC学习系列-后记 解决GET请求时中文乱码的问题 之前项目中的web.xml中的编码设置: <filter> <filter-name>CharacterEnc ...

  2. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  3. SpringMVC学习系列-后记 开启项目的OpenSessionInView

    在系列的 SpringMVC学习系列(12) 完结篇 的示例项目中,由于当时考虑到OpenSessionInView会对性能有一定的影响,所以就没有配置项目的OpenSessionInView.在ma ...

  4. SpringMVC学习系列-后记 结合SpringMVC和Hibernate-validator,根据后台验证规则自动生成前台的js验证代码

    在SpringMVC学习系列(6) 之 数据验证中我们已经学习了如何结合Hibernate-validator进行后台的数据合法性验证,但是通常来说后台验证只是第二道保险,为了更好的用户体验会现在前端 ...

  5. SpringMvc学习心得(五)控制器产生与构建

    SpringMvc学习心得(五)控制器产生与构建 标签: springspring mvc框架 2016-03-22 15:29 140人阅读 评论(0) 收藏 举报  分类: Spring(4)  ...

  6. springmvc学习笔记--REST API的异常处理

    前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...

  7. springmvc学习笔记---面向移动端支持REST API

    前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...

  8. SpringMVC:学习笔记(8)——文件上传

    SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...

  9. springmvc学习笔记(简介及使用)

    springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...

  10. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

随机推荐

  1. docker小demo

    http://www.blogjava.net/yongboy/archive/2013/12/12/407498.html

  2. NodeJS之express的路由浅析

    路由路径和请求方法一起定义了请求的端点,它可以是字符串.字符串模式或者正则表达式.后端在获取路由后,可通过一系列类似中间件的函数去执行事务. 可使用字符串的路由路径: // 匹配根路径的请求 app. ...

  3. (C/C++学习笔记) 十六. 预处理

    十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...

  4. swig的语法和用法

    1.swig一般在js文件里引入方法: __inline(*.tpl),如下图所示 2.特殊用法 swig模板里套用swig模板,<link rel="import" hre ...

  5. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165202

    2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165202 一.下载 虚拟机环境 Kali所需要虚拟机软件为VMware,在之前其他课程中使用的即为VMw ...

  6. <xsd:import>

    最近研究wsdl文件发现在生成客户端代码时报错了, WSDLException (at /wsdl:definitions/wsdl:types/xsd:schema): faultCode=OTHE ...

  7. Axure使用笔记1:如何去除IE中每次“已限制网页运行脚本或ActiveX控件”

    每次在Axure中画原型预览的时候,IE每次都有 这个比较烦,在Internent做如下设置,即可不再烦恼 看到没,给允许活动内容在我的计算机上的文件中运行打上勾

  8. 端到端文本识别CRNN论文解读

    CRNN 论文: An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Applica ...

  9. Android 引入外部模块编译选择

    /********************************************************************************* * Android 引入外部模块编 ...

  10. 30秒让让你的电脑快一倍 - 计算机基础 - 中国红客联盟 - Powered

    一.清理垃圾 在Windows在安装和使用过程中都会产生相当多的垃圾文件,包括临时文件(如:*.tmp.*._mp)日志文件(*.log).临时帮助文件(*.gid).磁盘检查文件(*.chk).临时 ...