【SpringMVC】XML配置说明
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配置说明的更多相关文章
- maven -- 学习笔记(二)之setting.xml配置说明(备忘)
setting.xml配置说明,learn from:http://pengqb.javaeye.com,http://blog.csdn.net/mypop/article/details/6146 ...
- SpringMvc xml 配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...
- spring-mvc.xml配置文件出错
在整合ssm三大框架的时候,配置spring-mvc.xml的文件的 <mvc:default-servlet-handler/> <mvc:annotation-driven /& ...
- 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 ...
- 当你的SSM项目中的springmvc.xml发生第一行错误解决方案
当你新建了一个SSM项目,你复制网上的xml文件来配置或者你下载了一个SSM项目打开发现xml文件错误,打开是第一行报错的时候你是不是很懵逼 或者是这样 总之就是xml文件中<?xml vers ...
- 关于springMVC中component-scan的问题以及springmvc.xml整理
关于springMVC中component-scan的问题以及springmvc.xml整理 一.component-scan问题和解决办法 最近在学习使用springMVC+myba ...
- java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'
今天在跑项目时遇到java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'报错,自己也是摸索了很久,一 ...
- springmvc.xml 中 <url-pattern></url-pattern>节点详解
1. 先来上段常见的代码 <!-- MVC Servlet --> <servlet> <servlet-name>springServlet</servl ...
- Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc.xml] cannot be opene
Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc. ...
随机推荐
- Java基础语法<九> 接口与内部类
1 接口 interface implement 接口的所有方法自动地属于public.因此,在接口中声明方法时,不必提供关键字public. 接口可以包含多个方法,接口中可以定义常量.接口中的 ...
- ASP.NET Core 开源论坛项目 NETCoreBBS
ASP.NET Core 轻量化开源论坛项目,ASP.NET Core Light forum NETCoreBBS 采用 ASP.NET Core + EF Core Sqlite + Bootst ...
- STL系列
STL—对象的构造与析构 STL—内存的配置与释放 STL—vector STL—vector空间的动态增长
- Python爬虫从入门到放弃(十八)之 Scrapy爬取所有知乎用户信息(上)
爬取的思路 首先我们应该找到一个账号,这个账号被关注的人和关注的人都相对比较多的,就是下图中金字塔顶端的人,然后通过爬取这个账号的信息后,再爬取他关注的人和被关注的人的账号信息,然后爬取被关注人的账号 ...
- JavaScript 值类型和引用类型的初次研究
今天遇到一个坑,具体的不多说,直接上代码 var a = [ [],[],[1,2,3] ] var b = ['颜色','大小','尺寸'] var arr = [] for(let i = 0; ...
- Hadoop 2.6.5 FileSystem和Configuration两个对象的探究
Hadoop 2.6.5 FileSystem和Configuration两个对象的探究 版权声明:本文为yunshuxueyuan原创文章,如需转载,请标明出处.[http://www.cnblog ...
- (转)Mysql数据库存储引擎
什么是MySql数据库 通常意义上,数据库也就是数据的集合,具体到计算机上数据库可以是存储器上一些文件的集合或者一些内存数据的集合. 我们通常说的MySql数据库,sql server数据库等 ...
- Objective-C 自定义UISlider滑杆 分段样式
效果 自定义一个功能简单的分段的滑杆 可显示分段名 为了显示效果,我们将滑块和节点都设置为不规则 这里只实现了分段的slider,未分段的没有实现,有兴趣的可以定义另一种类型做个判断修改下 需求分析 ...
- Linux系统运维工程该具备哪些素质
记得在上高中时,物理老师总是会对我们一句话:"学习是件苦差事."工作后发现,其实做运维也是件苦差事.最为一名运维工程师,深知这一行的艰辛,但和IT行业其他职务一样,那就是付出的越多 ...
- [补档][HNOI 2008]GT考试
[HNOI 2008]GT考试 题目 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字. 他的不吉利数学A1A2... ...