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

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进行java开发小demo

    http://www.codeceo.com/article/docker-java-development.html

  2. Pycharm(二)创建项目

    首次打开就是这样,可以创建新项目,打开一个项目,也可以从版本控制打开项目 我们就新建项目吧,Create New Project 创建项目就是这样了 Location:项目路径. Interprete ...

  3. python笔记02:列表与元素

    本章将引入一个新的概念:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合.这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构是序 ...

  4. redis下载安装

    在linux下下载redis $ wget http://download.redis.io/releases/redis-4.0.9.tar.gz 如果没有网络的话可以先下载安装包再移到linux虚 ...

  5. Locust 测试结果通过Matplotlib生成趋势图

    目的: 相信大家对于使用Loadrunner测试后的结果分析详细程度还是有比较深刻的感受的,每个请求,每个事务点等都会有各自的趋势指标,在同一张图标中展示.如下图: 而Locust自身提供的chart ...

  6. [编程题] N阶楼梯上楼问题

    import sys for line in sys.stdin: N=int(line.split()[0]) d1=1 d2=2 if(N==1): print 1 continue elif(N ...

  7. google-protobuf安装详解

    前言 编译调试项目的过程中涉及到caffe的编译,提示没有安装protobuf的错误,本文详解protobuf的安装: 问题描述 解决步骤 1.查看google protobuf的github,没有直 ...

  8. FZU OJ:2230 翻翻棋

    Problem 2230 翻翻棋 Accept: 872    Submit: 2132Time Limit: 1000 mSec    Memory Limit : 32768 KB  Proble ...

  9. web开发的一些总结

    现在我们是在互联网的时代,到处可以使用internet 这些年的发展,让we 成为了当前开发的主流,包括现在好多的移动端开发, 很多也是使用web 页面进行呈现,因为web 拉近了你我之间的距离.对于 ...

  10. Ribbon Control

    https://documentation.devexpress.com/#WindowsForms/CustomDocument2492 The Ribbon Control replaces tr ...