【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. ...
随机推荐
- Eclipse安装反编译工具JadClipse for Eclipse手把手教程
今天闲来无事准备弄弄eclipse的反编译工具JadClipse for Eclipse,百度经验里也说的比较清楚只是两个文件下载地址没有明确 net.sf.jadclipse_3.3.0.jar ...
- 20170713_filter/sort
js:filter过滤数组元素 //1.数组取奇数 var arr = [1,2,3,4,5]; var r = arr.filter(function(x){ return x % 2 !== 0; ...
- 51nod_1119:机器人走方格 V2
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 转化成杨辉三角就好辣@_@ #include< ...
- serv-u中如何映射网络驱动器
用一台机器作服务器,但硬盘空间不足,想通过影射网络驱动器来实现ftp的空间扩容! 如何映射网络硬盘或文件? 首先打开我的电脑--工具选项--映射网络驱动器: 如图示: 在文件夹输入映射路径:\\XXX ...
- guava缓存底层实现
摘要 guava的缓存相信很多人都有用到, Cache<String, String> cache = CacheBuilder.newBuilder() .expireAfterWrit ...
- 教你做炫酷的碎片式图片切换 (canvas)
前言 老规矩,先上 DEMO 和 源码.图片区域是可以点击的,动画会从点击的位置开始发生. 本来这个效果是我3年前做的,只是当是是用无数个 div 标签完成的,性能比较成问题,在移动端完全跑不动.最近 ...
- Linux安装搜狗输入法教程
最近开始学习linux 在安装输入法中遇到的一些问题,最终成功安装,也得益于网络上的前辈写的文章,现在将全部安装步骤以及遇到的一些问题总结如下: 基本上分三步走 1,添加fcitx的键盘输入法系统 ...
- ajax请求原理
首先分析使用ajax时候有那些不确定的因素 请求:1 请求的方式不确定 2 请求的地址不确定 3 请求是否异步不确定 4 发送的数据不确定 响应:5 返回的数据不确定 6 响应成功之后 需要处理的业务 ...
- vue组件的那些事($children,$refs,$parent)的使用
如果项目很大,组件很多,怎么样才能准确的.快速的寻找到我们想要的组件了?? 1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"&g ...
- Android对话框和帧动画
Android对话框 在一个例子中展示四种对话框. 设置四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk ...