上一章节中首先讲解前端控制器DispatcherServlet的配置包括加载springmvc文件、拦截什么样的请求等
还有两个组件:分别是适配器和映射器(另外再补充一组)

非注解的处理器映射器 处理器映射器:
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
另一个映射器:
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
补充:多个映射器可以并存,前端控制器判断url能让哪些映射器映射,就让正确的映射器处理。
非注解的处理器适配器:
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter 要求编写的Handler实现 Controller接口。
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter 要求编写的Handler实现 HttpRequestHandler接口。

可能有人没有配置上面的那些发现程序能够运行。那么知识点来了:
本章的第一个知识点在一个配置文件中是springmvc框架里面的一个配置文件,它的作用就是当没有配置上面的适配器和处理器框架会根据这个文件默认使用一组适配器和映射器。

我们来简单的看下这个文件里面的内容:

要先看下面这个翻译:
# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
这个就是默认加载的处理器映射器有两个:第一个是根据Bean的名字URL的映射器,第二个是默认的注解处理器映射器(等下会讲注解的)
org.springframework.web.servlet.HandlerMapping=
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,
\ org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
这个是默认加载的处理器适配器有三个:http请求相关的、简单的处理器适配器、默认的注解处理器适配器(等下会说到注解的)
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,
\ org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,
\ org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter org.springframework.web.servlet.HandlerExceptionResolver=
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,
\ org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,
\ org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

上面的文件里面涉及到两个注解的组件分别是:

注解的处理器映射器
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
注解的处理器适配器 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
这个两个是在spring3.1版本之前用的,在3.1之后使用的都是和http请求相关的两个组件
分别是:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

程序配置:注解的这个两个必须成对出现,要么全部出现、要么都不出现

<!-- 知识点二:=========注解映射器====================== -->
<!-- 说明 :对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配-->
<!-- ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method。 -->
<!-- 注意:从spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 知识点三:=========注解适配器====================== -->
<!-- 说明:注解式处理器适配器,对标记@ResquestMapping的方法进行适配。-->
<!-- 从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

实时程序:

@Controller
public class ItemList3 {
@RequestMapping("/queryItem")
public ModelAndView queryItem() {
// 商品列表
List<Items> itemsList = new ArrayList<Items>();
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(6088f);
items_2.setDetail("iphone6s苹果手机!");
itemsList.add(items_2);
// 创建modelAndView:填充数据、设置视图
ModelAndView modelAndView = new ModelAndView();
// 填充数据
modelAndView.addObject("itemsList", itemsList);// 类似request.setAttribute("","")
modelAndView.addObject("message", "Hello World!");
// 视图:逻辑名称
modelAndView.setViewName("/itemsList");// request.getRequestDispatcher("url").forward(request,// response);
return modelAndView;
}
}

当然光配置上面两个注解相关的组件还是不够的为什么呢?因为spring容器并不知道哪些类是处理器,这时候需要一个配置来把处理器交给容器管理。
知识点四:

<!-- 组件扫描器:可以扫描@Controller、@Service、@Repository等等 -->
<context:component-scan base-package="com.springapp.web.controller"/>

那么可能还有点懒说配置那两个注解相关的组件还是不方便,还是得写那么长。框架又帮我们想好了。
知识点五:真实开发的时候使用下面的配置可以代替上面配置的两个注解的组件

<!-- springmvc使用<mvc:annotation-driven> -->
<!-- 自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter, -->
<!-- 可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。 -->
<mvc:annotation-driven/>

这样以后这个配置文件中只要配置如下内容就可以了:
没有使用注解时候的配置文件:

<?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: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 ">
<!-- 配置简单的控制器处理适配器 -->
<!-- SimpleControllerHandlerAdapter:即简单控制器处理适配器, -->
<!-- 所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean作为Springmvc的后端控制器。 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<!-- 配置BeanNameUrl处理器映射器 -->
<!-- BeanNameUrlHandlerMapping:表示将定义的Bean名字作为请求的url,需要将编写的controller在spring容器中进行配置, -->
<!-- 且指定bean的name为请求的url,且必须以.action结尾。 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />
<!-- controller配置 -->
<!-- name="/items1.action":前边配置的处理器映射器为BeanNameUrlHandlerMapping, -->
<!-- 如果请求的URL 为“上下文/items1.action”将会成功映射到ItemList1控制器。 -->
<bean name="/items1.action" id="itemList1"
class="com.springapp.web.controller.ItemList1" />
<bean name="/items2.action" id="itemList2"
class="com.springapp.web.controller.ItemList2" />
<!-- 配置视图解析器 --> <!-- InternalResourceViewResolver:支持JSP视图解析 -->
<!-- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar包; -->
<!-- prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为: -->
<!-- 前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello, -->
<!-- 则最终返回的jsp视图地址 "WEB-INF/jsp/hello.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

使用注解之后的配置文件:

<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 "> <!-- ======================组件扫描器====================== -->
<context:component-scan base-package="com" /> <!-- springmvc使用<mvc:annotation-driven> -->
<!-- 自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter, -->
<!-- 使用<mvc:annotation-driven>替代注解处理器和适配器的配置。 -->
<mvc:annotation-driven /> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

作者: HansonQ
链接:http://www.imooc.com/article/4293
来源:慕课网

SpringMVC从入门到精通之第三章的更多相关文章

  1. SpringMVC从入门到精通之第四章

    第一个知识点:@Controller注解,用于标识这个类是一个后端控制器(类似struts中的action),主要作用就是接受页面的参数,转发页面.中间的业务逻辑是调用业务类处理的这个就是MVC设计模 ...

  2. ArcGIS for Desktop入门教程_第三章_Desktop软件安装 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第三章_Desktop软件安装 - ArcGIS知乎-新一代ArcGIS问答社区 1 软件安装 1.1 安装前准备 请确认已经收到来自Esri中国( ...

  3. D3.js的v5版本入门教程(第三章)—— 选择元素和绑定数据

    D3.js的v5版本入门教程(第三章) 在D3.js中,选择元素和绑定元素是最基本的内容,也是很重要的内容,等你看完整个教程后你会发现,这些D3.js教程都是在选择元素和绑定元素的基础上展开后续工作的 ...

  4. SpringMVC从入门到精通之第二章

    这一章原本我是想写一个入门程序的,但是后来仔细想了一下,先从下面的图中的组件用代码来介绍,可能更效果会更加好一点.第一节:开发准备介绍之前先说下我的开发调试环境:JDK 1.7的64位 .Eclips ...

  5. SpringMVC从入门到精通之第一章

    第一节 简介:SpringMVC是Spring框架的一个模块,Spring和SpringMVC无需通过中间整合层进行整合.SpringMVC是基于MVC的WEB框架.MVC设计模式在B/S下的应用: ...

  6. Python:从入门到实践--第三章--列表简介--练习

    #1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...

  7. Google C++测试框架系列入门篇:第三章 基本概念

    上一篇:Google C++测试框架系列入门篇:第二章 开始一个新项目 原始链接:Basic Concepts 词汇表 版本号:v_0.1 基本概念 使用GTest你肯定会接触到断言这个概念.断言是用 ...

  8. 《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解

    上一篇我们详细解释了一下SrpingMVC的执行流程以及一些默认的配置,在Spring的思想中,就是默认大于配置.今天我们来详细的研究一下DispatcherServlet的url-pattern配置 ...

  9. Node入门教程(4)第三章:第一个 Nodejs 程序

    第一个 Nodejs 程序 本教程仅适合您已经有一定的JS编程的基础或者是后端语言开发的基础.如果您是零基础,建议您先学一下老马的前端免费视频教程 第一步:创建项目文件夹 首先创建 demos 文件夹 ...

随机推荐

  1. HTML5 模拟现实物理效果,感受 Web 技术魅力

    Ball Pool 是一个基于 HTML5 技术的实验,模拟现实物理效果,让你在 Web 中感受自然物体的运动.玩法介绍:可以随意拖动圆球.点击页面背景.晃动浏览器.双击页面背景或者按住鼠标左键,有不 ...

  2. HTML5 表单新增属性

    1. 表单内元素的form属性 在H5中可以把form放到页面的任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定表单了 <form id=&quo ...

  3. JavaScript事件机制——细思极恐

    JavaScript事件机制,也有让人深思的东西.在一开始未深入了解,我头脑里有几个问题发出: 1. 自下而上(冒泡)事件怎么写,自上而下(捕获)又是怎么写? 2. 捕获型和冒泡型同时设置,谁生效? ...

  4. 原型prototype

    JS对象的比较 由于JS是解释执行的语言,那么代码中出现函数与对象如果重复执行,会创建多个副本 创建一个Person构造函数,要求有name,age,gender,sayHello 代码如下: fun ...

  5. jQuery中ajax的4种常用请求方式

    jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...

  6. Hadoop 2.5.1编译

    1.环境安装 gcc.gcc-c++.make.cmake.svn yum install lzo-devel zlib-devel gcc gcc-c++ make cmake autoconf a ...

  7. Android基础面试题

    1. 请描述一下Activity 生命周期. 答: 如下图所示.共有七个周期函数,按顺序分别是: onCreate(), onStart(), onRestart(), onResume(), onP ...

  8. SPS中使用JSOM发邮件

    直接上代码了: function ShowMailDialog() { $.ajax({ url: siteurl + "/_api/contextinfo", method: & ...

  9. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)

    Question 75You are designing a feature for a SharePoint 2010 solution that will be activated by defa ...

  10. 什么时候用Application的Context,什么时候用Activity的Context

    单例模式用application的context 如果我们在Activity A中或者其他地方使用Foo.getInstance()时,我们总是会顺手写一个『this』或者『mContext』(这个变 ...