首页 技术 正文
技术 2022年11月7日
0 收藏 427 点赞 289 浏览 3592 个字

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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289