Spring MVC的核心是DispatcherServlet,其初始化配置拥有一系列的组件。拥有默认的组件实现。
1.DispatcherServlet装配各型组件的逻辑
组件类型 | 发现机制 |
文件上传解析器 |
1)查找名为multipartResolver类型为MultipartResolver的Bean作为该类型组件 2)没有默认的实现类 如果用户没有在上下文中显式的定义这一类型的组件,DispatcherServlet将不会拥有该类型的组件。 |
本地化解析器 |
1)查找名为localeResolver类型为LocaleResolver的Bean作为该类型组件 2)如果1)找不到,使用默认的实现类(AcceptHeaderLocaleResolver)创建该类型组件 |
主题解析器 |
1)查找名为themeResolver类型为ThemeResolver的Bean作为该类型组件 2)如果1)找不到,使用默认的实现类(FixedThemeResolver)创建该类型组件 |
处理器映射器*(多个实例 order属性确定优先级) |
1)如果detectAllHandlerMappings属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为HandlerMapping的Bean,将他们作为该类型组件 2)如果detectAllHandlerMappings属性为false,查找名为handlerMapping,类型为HandlerMapping的的Bean作为该类型组件 3)如果通过以上两种方式都找不到,使用BeanNameUrlHandlerMapping实现类创建该类型组件 |
处理器适配器*(多个实例 order属性确定优先级) |
1)如果detectAllHandlerAdapters属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为HandlerAdapter的Bean,将他们作为该类型组件 2)如果detectAllHandlerAdapters属性为false,查找名为handlerAdapter,类型为HandlerAdapter的的Bean作为该类型组件 3)如果通过以上两种方式都找不到,使用DispatcherServlet.proeprties配置文件中指定的三个实现类分别创建一个适配器,添加到适配器列表中 |
处理器异常解析器(多个实例 order属性确定优先级) |
1)如果detectAllHandlerExceptionResolvers属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为HandlerExceptionResolver的Bean,将他们作为该类型组件 2)如果detectAllHandlerExceptionResolvers属性为false,查找名为handlerExceptionResolver类型为HandlerExceptionResolver的的Bean作为该类型组件 3)如果通过以上两种方式都找不到,使用DispatcherServlet.proeprties默认实现类 |
视图名称翻译器 |
1)查找名为viewNameTranslator;类型为RequestToViewNameTranslator的Bean作为该类型组件 2)如果1)找不到,使用默认的实现类(DefaultRequestToViewNameTranslator)创建该类型组件 |
视图解析器(多个实例 order属性确定优先级) |
1)如果detectAllResolvers属性为true,默认为true根据类型匹配机制查找上下文及父Spring容器中所有类型为ViewResolver的Bean,将他们作为该类型组件 2)如果detectAllResolvers属性为false,查找名为viewResolver,类型为ViewResolver的的Bean作为该类型组件 3)如果通过以上两种方式都找不到,使用DispatcherServlet.proeprties配置文件中定义的默认实现类(InternalResourceViewResolver)创建该类型组件 |
Spring MVC
C:Contorller ,Spring 3.0支持以注解方式的驱动器,使得普通POJO类可以成为处理HTTP请求的控制器
@Controller public class UserContorller{ @RequestMapping("user/register") public String register(){ return "user/register"; } @RequestMapping("{userId}") public ModelAndView showDetail(@PathVariable("userId")String userId){ ModelAndView mav=new ModelAndView(); mav.setViewName("user/showDetail"); mav.addObject("user",userService.getUserId(userId)); return mav; } }
@RequestMapping不仅支持标准URL,还支持Ant风格的?* ** {XXX},占位符URL是Spring3.0新增功能
占位符可以通过@PathVariable绑定到方法的入参中
@RequestMapping(value="/delete",method=RequestMethod.Post,params="userId",headers="content-type=text/*")
@RequestMapping除了可以使用请求URL映射请求外,还可以使用请求方法,请求头参数和请求参数(报文体和URL包含的请求参数)映射请求。
value->请求URL method->请求方法 params->请求参数 headers->报文头
params headers支持简单的表达式 params演示:
- param1 包含
- !param1 不包含
- param1!=value1 包含param1的请求参数,其值不能为value1
Spring MVC处理方法
处理方法签名详细说明:(设置方法入参绑定请求信息,定义返回值类型,Spring MVC对不同签名的处理方法如何进行调用等)
@RequestParam绑定请求参数值 属性 value required defaultValue
@CookieValue 绑定请求中的Cookie值 属性 value required defaultValue
@RequestHeader 绑定请求报文头的属性值 属性 value required defaultValue
使用命令、表单对象绑定请求参数值 POJO类拥有若干属性,会自动装配
使用Servlet API对象 HttpServletRequest HttpServletResponse HttpSession
Spring MVC定义了代理类 WebRequest NativeWebRequest
使用IO对象作为入参 (即OutputStream=>ServletResponse.getOutputStream()
Writer=>Serv;etResponse.getWriter() InputStream=>ServletRequest.getInputStream() Reader=>ServletRequest.getReader())
其他类型参数 如Locale=> HttpServletRequest.getLocale()
Principle=>HttpServletRequest.getUserPrincipal()
2.HttpMessageConverter<T>
Spring3.0新增的重要接口,他负责将请求信息转换为一个对象(类型T),将对象(类型T)输出为响应信息
public interface HttpMessageConverter<T> { /** * Indicates whether the given class can be read by this converter. * @param clazz the class to test for readability * @param mediaType the media type to read, can be {@code null} if not specified. * Typically the value of a {@code Content-Type} header. * @return {@code true} if readable; {@code false} otherwise */ boolean canRead(Class<?> clazz, MediaType mediaType); /** * Indicates whether the given class can be written by this converter. * @param clazz the class to test for writability * @param mediaType the media type to write, can be {@code null} if not specified. * Typically the value of an {@code Accept} header. * @return {@code true} if writable; {@code false} otherwise */ boolean canWrite(Class<?> clazz, MediaType mediaType); /** * Return the list of {@link MediaType} objects supported by this converter. * @return the list of supported media types */ List<MediaType> getSupportedMediaTypes(); /** * Read an object of the given type form the given input message, and returns it. * @param clazz the type of object to return. This type must have previously been passed to the * {@link #canRead canRead} method of this interface, which must have returned {@code true}. * @param inputMessage the HTTP input message to read from * @return the converted object * @throws IOException in case of I/O errors * @throws HttpMessageNotReadableException in case of conversion errors */ T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException; /** * Write an given object to the given output message. * @param t the object to write to the output message. The type of this object must have previously been * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}. * @param contentType the content type to use when writing. May be {@code null} to indicate that the * default content type of the converter must be used. If not {@code null}, this media type must have * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have * returned {@code true}. * @param outputMessage the message to write to * @throws IOException in case of I/O errors * @throws HttpMessageNotWritableException in case of conversion errors */ void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException; }
HttpMessageConverter<T>实现类
实现类 | 功能说明 |
StringHttpMessageConverter |
用途:将请求信息转为字符串 1)T为String类型 2)可读取所有媒体类型(*/*)的请求信息,可以通过设置supportedMediaTypes属性指定媒体类型 3)响应信息的媒体类型为text/plain(即Content-Type的值) |
FormHttpMessageConverter |
用途:将表单数据读取到MultiValueMap中 1)T为org.springframework.util.MultiValueMap<String,?>类型 2)支持读取application/x-www-form-urlencoded的媒体类型,但不支持读取multipart/form-data的媒体 3)可写application/x-www-form-urlencoded及multipart/form-data媒体类型的响应信息 |
XmlAwareFormHttpMessageConverter | 拓展与FormHttpMessageConverter,如果部分表单属性是Xml数据,可用改转换器读取 |
ResourceHttpMessageConverter |
用途:读写org.srpingframework.core.io.Resource对象 1)T类型为org.srpingframework.core.io.Resource类型 2)可以读取所有媒体类型(*/*)的请求信息 3)如果类路径下提供了JAF(Java Activation Framework),则根据Resource的类型指定响应的媒体类型,否则响应媒体类型为application/octet-stream |
BufferedImageHttpMessageConverter |
用途:读写BufferedImage对象 1)T类型为BufferedImage类型 2)可以读取所有媒体类型 3)返回BufferedImage相应的媒体类型,也可以通过contentType显示指定 |
ByteArrayHttpMessageConverter |
用途:读写二进制数据 1)T为byte[]类型 2)可以读取所有的媒体类型(*/*)的请求信息,可通过设置supportedMediaTypes属性指定媒体类型 3)响应媒体类型为application/octet-stream |
HttpMessageConverter | |
HttpMessageConverter | |
HttpMessageConverter | |
HttpMessageConverter | |
HttpMessageConverter | |
HttpMessageConverter |
相关推荐
简易的Spring mvc demo,里面涵盖了很多的知识点,并且有备注,有搭建项目的说明,适合初次接触SpringMVC的人。
奉上学习Spring MVC 3.0的技术资料PPT资料,我花了两天写成,该资料根据最新拙作的《Spring 3.x 企业应用开发实战 》写成,仅需要1个小时左右的时间,就可以让你学习到Spring MVC 3.0的所有知识点和新功能,强烈推荐...
- Spring MVC常见知识点及源码解析 - MVC 是什么 / 有什么优点? - 什么是 Spring MVC? - Spring MVC的优缺点? - 什么是DispatcherServlet? - Spring MVC有哪些组件?(见:DispatcherServlet源码) - 简述...
针对spring mvc设计模式中的概念性知识点进行简单易懂的讲解
2022年最新Java面试题整理,全网最全一份,包含各个技术栈,Java基础,Java集合,多线程,...MySQL,Nginx,rabbitmq,Redis,Tomcat,zookeeper,设计模式,数据结构,算法,分布式,限流,降级,cap原理等等知识点
关于spring mvc 的一些知识点累计的ppt 希望能帮到大家
本教程完整地讲解了Spring MVC的每个知识点,包括独立运行的Demo示例,欢迎一起交流学习。Java学习交流群:239126529 教程软件版本说明: 1)Spring MVC框架:spring-framework-5.0.2.RELEASE 2)开发工具:IntelliJ...
Spring MVC 知识点总结,很全,包括概念、框架、框架具体流程、应用、组件、环境搭建、开发步骤、注解、数据校验等。
使用maven集成jar包的方式,搭建基于spring mvc,spring,mybatis的一个练习小demo,可帮助练习三大框架的搭建,demo设计的小知识点也比较多,登录的拦截器、aop的切面配置、国际化、以及.properties文件中敏感内容的...
本书针对每一个知识点都通过相应的程序给出了示范,所有应用范例都密切契合企业开发实际场景,例如用户权限验证、文件上传下载等都是企业开发实际功能,同时采用了目前企业流行、规范的开发架构,严格遵守Java EE...
主要为学习SSM途中的知识点和问题的记录,包含详细过程, 适用于初学JavaWeb的兄弟姐妹们 方便进行验证性学习和敲代码, 额,笔者我是参考b站视频学习后记录的。
springmvc的思维导图,完整版, Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。
项目对spring如何整合springmvc进行了详细地注释和介绍,并且包含了很多知识点,比如避免重复扫描包,spring IOC的父子关系和请求筛查等。博客地址:http://blog.csdn.net/qwlzxx/article/details/73732292
java开发工程师面试中被经常问到知识点大全,pdf文档,本文分为十九个模块,分别是∶ JavaSE基础、容器、多线程、反射、对象拷贝 Java Web相关的(servlet,JSP)、异常、网络、常见的23种设计模式 java的spring相关...
SpringMVC理论知识点,包括spring自动装配、spring事件处理、依赖注入、spring事务管理、AOP代理、AOP (部分,并非某个方面所有知识)
在本篇文章里小编给大家整理了关于Spring MVC的工作原理的相关知识点以及实例代码内容,需要的朋友们可以参考下。
2020年最新Java面试题整理,全网最全一份,包含各个技术栈,Java基础,Java集合,多线程,...MySQL,Nginx,rabbitmq,Redis,Tomcat,zookeeper,设计模式,数据结构,算法,分布式,限流,降级,cap原理等等知识点
个人对Spring Mvc知识点的总结,帮助初学者更好理解,帮助老手回顾知识.
本书的示例都经笔者精心挑选,具有很强的针对性,力求让读者可以明白Spring每个知识点。最后的两个综合案例,采用最科学的轻量级J2EE结构,涉及的框架有Spring,Struts,WebWork2,Hibernate,Free Marker,Velocity...
该系统主要功能包括:用户管理、考试管理、试题新增、试题查询、分类管理。 用户管理:批量导入用户、批量操作用户、修改用户信息。 ... 主观题阅卷、查看成绩;... 分类管理:对课程和知识点进行增、删、改、查的操作。