springmvc流程:前端控制器(DispatcherServlet)-->映射器HandlerMapping-->适配器HandlerAdapter-->视图解析器ViewResolver这四个流程缺一不可

真正内涵:

控制器通过映射器得到url所对应的bean,进而去调用适配器(被调用)来执行bean(被执行),得到的调用结果,通过视图解析器来处理。

下面是springmvc xml配置的内容,看配置文件时,大脑中联想着这四个流程:

前端控制器(web.xml中已写出)-->映射器(注解/非注解)->适配器(注解/非注解)->视图解析器(只有一个)

说明:对于映射器、适配器,要保证注解时同时用注解。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 配置handler -->
	<bean id="userController" name="/stu.do" class="com.z.controller.UserController"/>
	<bean name="/userRequest.do" class="com.z.controller.UserRequestHandler"/>

	<!-- 对于注解的Handler可以单个配置 在实际开发中使用组件扫描
		可以扫描controller、service...
		这里扫描controller,指定controller包
	 -->
	<context:component-scan base-package="com.z.controller"/>

	<!-- 处理器映射器1  把BEAN的NAME当成url进行查找,需要在配置handler时指定beanname就是url-->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

	<!-- 处理器映射器2  简单url映射 -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/items1.do">userController</prop>
				<prop key="/items2.do">userController</prop>
			</props>
		</property>
	</bean>

	<!-- 处理器映射器3 注解开发 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

	<!-- 处理器适配器1  非注解 所有适配器实现了HandlerAdapter接口 执行查找到的处理器 -->
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

	<!-- 处理器适配器2 非注解 -->
	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

	<!-- 处理器适配器3 注解  spring-webmvc-4.3.4  since3.1 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

	<!--mvc:annotation-driven代替上边注解映射器和注解适配器
		mvc-annotation-driven默认加载很多参数绑定方法
		比如json解析器就默认加载了,
		如果使用mvn-annotation-driven就不用配置 注解开发映射器 注解开发适配器
	 -->
	<mvc:annotation-driven/>

	<!-- 注解的映射器和适配器要配对使用 -->

	<!-- 视图解析器 解析jsp,默认使用jstl标签,需要引入jstl包-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

精简版:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 简化版 全部使用注解开发
		对于注解的Handler可以单个配置 在实际开发中使用组件扫描
		可以扫描controller、service...
		这里扫描controller,指定controller包
	 -->
	<context:component-scan base-package="com.z"/>
	<!-- mvn:annotation-driven取代了注解的映射器和适配器,并且可以处理json数据 -->
	<mvc:annotation-driven/>
	<!-- 视图解析器 解析jsp,默认使用jstl标签,需要引入jstl包-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

【SpringMVC】XML配置说明的更多相关文章

  1. maven -- 学习笔记(二)之setting.xml配置说明(备忘)

    setting.xml配置说明,learn from:http://pengqb.javaeye.com,http://blog.csdn.net/mypop/article/details/6146 ...

  2. SpringMvc xml 配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...

  3. spring-mvc.xml配置文件出错

    在整合ssm三大框架的时候,配置spring-mvc.xml的文件的 <mvc:default-servlet-handler/> <mvc:annotation-driven /& ...

  4. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

  5. 当你的SSM项目中的springmvc.xml发生第一行错误解决方案

    当你新建了一个SSM项目,你复制网上的xml文件来配置或者你下载了一个SSM项目打开发现xml文件错误,打开是第一行报错的时候你是不是很懵逼 或者是这样 总之就是xml文件中<?xml vers ...

  6. 关于springMVC中component-scan的问题以及springmvc.xml整理

    关于springMVC中component-scan的问题以及springmvc.xml整理 一.component-scan问题和解决办法         最近在学习使用springMVC+myba ...

  7. java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'

    今天在跑项目时遇到java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'报错,自己也是摸索了很久,一 ...

  8. springmvc.xml 中 <url-pattern></url-pattern>节点详解

    1.  先来上段常见的代码 <!-- MVC Servlet --> <servlet> <servlet-name>springServlet</servl ...

  9. Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc.xml] cannot be opene

                        Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc. ...

随机推荐

  1. iOS 图文并茂的带你了解深拷贝与浅拷贝

    一.概念与总结 1.浅拷贝 浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间,当内存销毁的时候,指向这片内存的几个指针需要重新定义才可以使用,要不然会成为野指针. 浅拷贝就是拷贝指 ...

  2. 还在为画“类Word文档报表”而发愁吗?

    欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~​ Word 是非常强大的文档编辑工具,一些行业制式文档都是使用Word来创建的,像教育行业的申请表,履历表,审批表等,像石油业的 ...

  3. js冒泡排序,数组去重

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Delphi Screen.DataModuleCount 总是返回 0!Delphi 的 Bug? DataModuleCount = 0

         今天遇到一个很隐蔽的 Delphi 问题,不知做了什么,有一个功能总是不能使用,后来跟踪以下发现是因为 Screen.DataModuleCount 总是返回 0,而程序中一个函数正好要用到 ...

  5. [luogu P1967][NOIp2013] 货车运输

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

  6. Unity 工作经历+近期面试经历

    由于团队解散,这最近都在找新工作机会--投简历找工作.已经面试三家了,都没拿到offer,挺失落的.把这种感受记录下来,以作后鉴. 这本质上是一篇面试经历的记录,并不是什么面试攻略,主要是给自己总结的 ...

  7. (转)XML中必须进行转义的字符

    场景:在工作中接触到很多xml文件,为了更好的操作这些文件,所有很有必要熟知xml文件的相关语义. 1 引入 编写XML代码经常遗漏的常识: XML实体中不允许出现"&", ...

  8. 不借助工具在浏览器中通过Web API执行Dynamics 365操作(Action)实例

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复262或者20170727可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  9. 用subline text写PHP后台服务器POST请求

    1 运行XAMPP程序,看到Apache Web Server 是Running状态即可 2 打开Subline text ,新建一个PHP文件,选择保存路径:应用程序->XAMPP->h ...

  10. log4g 使用教程

    日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...