Spring MVC知识点整理
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>platform-services</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:platform-services-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
/**
* Process the actual dispatching to the handler.
* <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
* The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
* to find the first that supports the handler class.
* <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
* themselves to decide which methods are acceptable.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
*/
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null ;
int interceptorIndex = -1; try {
ModelAndView mv;
boolean errorView = false; try {
processedRequest = checkMultipart(request); // Determine handler for the current request.
mappedHandler = getHandler(processedRequest, false);
if ( mappedHandler == null || mappedHandler.getHandler() == null ) {
noHandlerFound( processedRequest, response);
return;
} // Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler .getHandler()); // Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals( method);
if ( isGet || "HEAD".equals( method)) {
long lastModified = ha.getLastModified( request, mappedHandler.getHandler());
if ( logger.isDebugEnabled()) {
String requestUri = urlPathHelper.getRequestUri(request );
logger.debug( "Last-Modified value for [" + requestUri + "] is: " + lastModified);
}
if ( new ServletWebRequest( request, response).checkNotModified( lastModified) && isGet) {
return;
}
}
1. 在handler执行之前,获取注册到相关handler的interceptor列表,调用的顺序与配置的顺序一致
// Apply preHandle methods of registered interceptors.
HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
if ( interceptors != null) {
for ( int i = 0; i < interceptors. length; i++) {
HandlerInterceptor interceptor = interceptors[ i];
if (!interceptor.preHandle(processedRequest , response , mappedHandler.getHandler())) {
2. 如果interceptor.preHandle执行结果为false,则触发afterCompletion方法的调用,并直接返回
triggerAfterCompletion( mappedHandler, interceptorIndex , processedRequest, response , null);
return;
}
interceptorIndex = i;
}
} // Actually invoke the handler.
mv = ha.handle( processedRequest, response, mappedHandler.getHandler()); // Do we need view name translation?
if ( mv != null && ! mv.hasView()) {
mv.setViewName(getDefaultViewName( request));
}
3. handler执行完业务逻辑处理后,逆序调用interceptor列表来执行postHandle方法
// Apply postHandle methods of registered interceptors.
if ( interceptors != null) {
for ( int i = interceptors. length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[ i];
interceptor.postHandle( processedRequest, response, mappedHandler.getHandler(), mv );
}
}
}
catch (ModelAndViewDefiningException ex) {
logger.debug( "ModelAndViewDefiningException encountered" , ex);
mv = ex.getModelAndView();
}
catch (Exception ex) {
Object handler = ( mappedHandler != null ? mappedHandler.getHandler() : null );
mv = processHandlerException(processedRequest , response , handler , ex);
errorView = ( mv != null);
} // Did the handler return a view to render?
if ( mv != null && ! mv.wasCleared()) {
render( mv, processedRequest, response);
if ( errorView) {
WebUtils. clearErrorRequestAttributes(request);
}
}
else {
if ( logger.isDebugEnabled()) {
logger.debug( "Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +
"': assuming HandlerAdapter completed request handling");
}
}
4. 完成视图的渲染之后,触发interceptor的afterCompletion方法,逆序执行
// Trigger after-completion for successful outcome.
triggerAfterCompletion( mappedHandler, interceptorIndex, processedRequest , response , null);
} catch (Exception ex) {
5. 触发Exception后,执行interceptor的afterCompletion方法
// Trigger after-completion for thrown exception.
triggerAfterCompletion( mappedHandler, interceptorIndex, processedRequest , response , ex );
throw ex;
}
catch (Error err) {
ServletException ex = new NestedServletException("Handler processing failed" , err );
// Trigger after-completion for thrown exception.
triggerAfterCompletion( mappedHandler, interceptorIndex, processedRequest , response , ex );
throw ex;
} finally {
// Clean up any resources used by a multipart request.
if ( processedRequest != request) {
cleanupMultipart( processedRequest);
}
}
}
<mvc:interceptors >
<bean class= "com.interceptor.test.authenInterceptor" >
<property name ="excludeUris">
<list >
<value >/static/ </value >
</list >
</property >
</bean >
</mvc:interceptors >
<bean name="/hello.htm" class="com.sina.controller.HelloController"/> <bean name="/sayHello*" class="com.sina.controller.HelloController"/>
SimpleUrlHandlerMapping相对于BeanNameUrlHandlerMapping的优势在与不需要为一个类重复定义bean,而且XML的可读性也更强,其配置实例如下:
<bean id="helloController" class="com.sina.controller.HelloController"/> <bean id="urlHandler" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/hello.htm" value-ref="helloController"/>
<entry key="/sayHello*" value-ref="helloController"/>
<entry key="/welcome.html" value-ref="helloController"/>
<entry key="/welcomeUser*" value-ref="helloController"/>
</map>
</property>
</bean>
/**
* Initialize the HandlerMappings used by this class.
* <p>If no HandlerMapping beans are defined in the BeanFactory for this namespace,
* we default to BeanNameUrlHandlerMapping.
*/
private void initHandlerMappings (ApplicationContext context) {
this. handlerMappings = null; if ( this. detectAllHandlerMappings) {
//1. 如果detectAllHandlerMappings,则找到所有注册的HandlerMapping bean
Map<String, HandlerMapping> matchingBeans =
BeanFactoryUtils.beansOfTypeIncludingAncestors( context, HandlerMapping.class, true , false);
if (! matchingBeans.isEmpty()) {
this. handlerMappings = new ArrayList<HandlerMapping>(matchingBeans .values());
//2. 对handlerMappings进行排序,排序依据是注册时的order值
OrderComparator.sort(this.handlerMappings);
}
}
//3. 如果detectAllHandlerMappings为false,则只查找名称为HandlerMapping的bean。
else {
try {
HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME , HandlerMapping.class);
this. handlerMappings = Collections.singletonList(hm);
}
catch ( NoSuchBeanDefinitionException ex) {
// Ignore, we'll add a default HandlerMapping later.
}
} // Ensure we have at least one HandlerMapping, by registering
// a default HandlerMapping if no other mappings are found.
4. 如果没有在上下文找到一个HandlerMapping bean,则采用默认策略加载,具体策略设置可以在 DispatcherServlet.properties中看到
if ( this. handlerMappings == null) {
this. handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
if ( logger.isDebugEnabled()) {
logger.debug( "No HandlerMappings found in servlet '" + getServletName() + "': using default");
}
}
}
HandlerAdapter的作用是调用HandlerMapping映射好的处理类和方法,具体配置方式和初始化流程与HandlerMapping相似。
HandlerExceptionResolver接口让开发者可以对异常进行处理,返回给调用者更友好、清晰的信息。
public final void init()
throws ServletException
{
if(logger.isDebugEnabled())
logger.debug((new StringBuilder()).append("Initializing servlet '").append(getServletName()).append("'").toString());
try
{
org.springframework.beans.PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
org.springframework.core.io.ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(org/springframework/core/io/Resource, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch(BeansException ex)
{
logger.error((new StringBuilder()).append("Failed to set bean properties on servlet '").append(getServletName()).append("'").toString(), ex);
throw ex;
}
initServletBean();
if(logger.isDebugEnabled())
logger.debug((new StringBuilder()).append("Servlet '").append(getServletName()).append("' configured successfully").toString());
}
Spring MVC知识点整理的更多相关文章
- Spring MVC 知识点整理
extend:http://www.jianshu.com/p/bef0e52067d2 1. Redis 存储方式 Redis存储机制分成两种Snapshot 和 AOF.无论是那种机制,Redis ...
- 2019年Spring核心知识点整理,看看你掌握了多少?
前言 如今做Java尤其是web几乎是避免不了和Spring打交道了,但是Spring是这样的大而全,新鲜名词不断产生,学起来给人一种凌乱的感觉,在这里总结一下,理顺头绪. Spring 概述 Spr ...
- [Java] Spring MVC 知识点
云图: @Service 用于标注业务层组件. 在 Spring-servlet 配置xml中,component-scan 标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Compone ...
- Spring MVC面试整理
Spring MVC执行过程 客户端的请求提交到dispatcherServlet DispatcherServlet查询一个或者多个handlermapping ,找请求的Controller Di ...
- Spring mvc知识点总结——面试篇
一.MVC思想MVC(Model-View-Controller)三元组的概念:1.Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数 ...
- Spring重要知识点整理
一.IOC(Inversion of Control):控制反转 (1)Spring Core最核心部分 (2)需要先了解依赖注入(Denpendency Injection)/把底层类作为参数传递给 ...
- spring MVC 异常处理整理
一.异常结构 1.异常由外到内如下: web服务器处理异常--->web应用里面web.xml处理异常--->Spring框架处理异常---->控制器注释处理异常--->控制器 ...
- Spring MVC 知识点记忆
1.Dao 用的 @Repository 2.Handler 用的 @Controller 3. @Autowired 消除了对get set方法 4. @RequestMapping(value= ...
- spring mvc 注解整理(一)
@Controller和@RestController: RestController = @ResponseBody + @Controller 所有返回都是json类型,无法跳转到jsp页面,但 ...
随机推荐
- 常用路径 URL 中的斜杠与反斜杠
常用路径中的斜杠与反斜杠... ------------------------------ 斜杠:反斜杠:======================电脑能识别的斜杠有两种:斜杠分正斜杠(forwa ...
- Android学习之旅(一)
2017-02-27 今天开始,正式开启Android学习之旅,背景从事.Net平台开发快五年了,一直在用C#做Web开发. 前天选购了两本书:<Java 编程思想(第四版)>和<第 ...
- 【转载】webstorm11(注册,激活,破解,码,一起支持正版,最新可用)(2016.11.16更新)
很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 最近封的厉害仅作测试 选择 License ...
- JS小福利 —— 实时更新的页面小时钟
今天小女刚学会了一个好玩的小玩意儿~~特来跟大家分享一下~~~ 这是一个有趣的时钟显示程序,可以进行实时的年月日.星期.时分秒更新,有了这组小代码,以后可以作为日期插件应用在大型的JS代码中哦~~ 积 ...
- struts2--Action
HTTP请求 提交 Struts2 StrutsPrepareAndExecuteFilter 核心控制器 -- 请求分发给不同Action Action书写的的三种格式 第一种 Action可以是 ...
- Tensorflow之MNIST解析
要说2017年什么技术最火爆,无疑是google领衔的深度学习开源框架Tensorflow.本文简述一下深度学习的入门例子MNIST. 深度学习简单介绍 首先要简单区别几个概念:人工智能,机器学习,深 ...
- python2.x与3.x的主要区别笔记
#coding:utf-8 ''' python3.x新的东西 目录 使用__future__模块 print函数 整数除法 Unicode xrange 触发异常 处理异常 next()函数和.ne ...
- C和C++混合编程之 extern “C”的使用
C和C++混合编程之 extern "C"的使用 首先要明白: C++号称是C语言的超集,也确实,从语言的基本语法上,C++是包含所有C语言的语法的,而且C++为了兼容C,连C语言 ...
- 小而美的 React Form 组件
背景 之间在一篇介绍过 Table 组件< React 实现一个漂亮的 Table > 的文章中讲到过,在企业级后台产品中,用的最多且复杂的组件主要包括 Table.Form.Chart, ...
- 使用spring框架处理编码问题
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp90 我们在开发时,经常要对中文字符进行处理,进行处理中文字符的方式也 ...