About SpringMVC interceptors and exception

A. Upload file

1. File Upload

  • SpringMVC provides direct support for file uploads, this type is by MultipartResolver plug and play technology. Spring with Jakarta Commons FileUpload technology to achieve a MultipartResolver implementation class: CommonsMultipartResolver.
  • SpringMVC default context is not equipped MultipartResolver, so the default file upload work can not deal with the case, if you want to use Spring file upload function, the need is now configured MultipartResolver context.

    2. Configure MultipartResolver

  • defaultEncoding: JSP user must pageEncoing properties of the same, in order to resolve the contents of the form are correct.
  • To make CommonsMulitpartResolver work properly, you must first class and Jakarta Commons FileUpload package Jakarta Commons io added to the classpath.

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <propert name="defaultEncoding" value="UTF-8"></property>
    <property name="maxUploadSize" value="524880"></property>
</bean>

II. Interceptors

1. custom interceptor

SpringMVC interceptors may be used to intercept the request process, the user can custom interceptor to perform specific functions, custom interceptor must implement HandlerInterceptor interface. Interface has three methods:

  • preHandle (): This method is called prior to the processor requesting service, in which method a user request to process the request. If you decide to request the interceptor to intercept deal also call other interceptors, or service processor to carry out processing, it returns true; if the programmer decides not need to call other components to process the request, it returns false.

  • postHandle (): This method has processed the service request, but DispatcherServlet returned to the client until a response is invoked, in this process the user request request () processing.

  • afterCompletion (): This method is completely processed after completion DispatcherServlet request call, you can do some clean-up operation resources in the process.

2. interceptor method execution sequence

3. configure a custom interceptor

<mvc:interceptors>
      <!--  拦截所有资源 -->
      <bean class="com.desperado.interceptors.FirstInterceptor"></bean>
      <!--拦截指定资源-->
      <mvc:interceptor>
              <mvc:mapping path="/emps"/>
              <bean class="com.desperado.interceptors.SecondInterceptor"></bean>
      </mvc:interceptor>
      <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>

4. The order of execution of a plurality of interceptors

5. The preHandle interceptor () method returns false when the execution sequence

6. a classic face questions

Interceptors and filter What is the difference:

  1. Interceptor java reflection mechanism is based, and the filter is based on the callback function.
  2. Interceptor does not depend on the Servlet container, and the filter dependent on the Servlet container.
  3. Action can only request interceptor function, and the filter may be their effects on nearly all requests.
  4. Interceptors can access the context of the action, the value of the stack inside the object, and the filter can not be accessed.
  5. In a statement the cycle of action, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized (call here again, is for the constructor, and the request will match doFilter do ongoing process.)
  6. Interceptor can acquire IOC container Bean, and the filter not.

Execution order

III. Exception Handling

1. Exception Handling

  • Spring MVC HandlerExceptionResolver by exception handlers, including mapping Handler, the data and the target binding method performed when the abnormality occurs.
  • SpringMVC提供的HandlerExceptionResolver的实现类
    - AbstractHandlerExceptionResolver
    - AbstractHandlerMethodExceptionResolver
    - ExceptionHandlerExceptionResolver
    - AnnotationMethodHandlerExceptionResolver
    - DefaultHandlerExceptionResolver
    - ResponseStatusExceptionResolver
    - SimpleMappingExceptionResolver
    - HandlerExceptionResolverComposite

2.HandlerExceptionResolver

  • DispatcherServlet默认装配的HandlerExceptionResolver:
    • 没有使用
      -AnnotationMethodHandlerExceptionResolver
      -DefaultHandlerExceptionResolver
      -ResponseStatusExceptionResolver

    • 使用了
      -ExceptionHandlerExceptionResolver
      -DefaultHandlerExceptionResolver
      -ResponseStatusExceptionResolver

3.ExceptionHandlerExceptionResolver

  • 主要处理Handler中用@ExceptionHandler注解定义的方法。
  • @ExceptionHandler注解定义的优先级问题:如果发生的是NullPointerException,但是声明的异常有RuntimeException和Exception,此时会根据异常的最近继承关系找到继承深度最浅的那个@ExceptionHandler注解方法,即标记了RuntimException的方法。
  • ExceptionHandlerMethodResolver内部若找不到@ExceptionHandler注解的话,会找@ControllerAdvice中的@ExceptionHandler注解方法

4.ResponseStatusExceptionResolver

  • 在异常及异常父类中找到@ResponseStatus注解,然后使用这个注解的属性进行处理。
  • 定义一个@ResponseStatus注解修饰的异常类。
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public class UnAuthorizedException extends RuntimeException{}
  • 若在处理器方法中抛出了上面定义的异常:
    由于触发的异常带有@ResponseStatus注解,因此会被ResponseStatusExceptionResolver解析到。最后响应HttpStatus.UNAUTHORIZED代码给客户端。

5.DefaultHandlerExceptionResolver

对一些特殊的异常进行处理,比如:
NoSuchRequestHandlingMethodException
HttpRequestMethodNotSupportedException
HttpMediaTypeNotSupportedException
HttpMediaTypeNotAcceptableException
等。

6.SimpleMappingExceptionResolver

如果希望对所有异常进行统一处理,可以使用该解析器,它将异常类名映射为视图名,即发生异常是使用对应的视图报告异常

四.Spring的运行流程

  1. 请求到达Spring DispatcherServlet的url-pattern。
  2. 判断SpringMVC中是否存在对应的映射
  3. 如果不存在判断是否配置了
  4. 如果配置了就去找目标资源,如果没有配置,返回404页面。
  5. 如果存在则从HandlerMapping获取handlerExecutionChain对象。
  6. 获取HandlerAdapter对象。
  7. 调用拦截器的preHandle方法。
  8. 调用目标Handler的目标方法得到ModelAndView对象。
  9. 调用拦截器的postHandle方法。
  10. 判断是否存在异常
  11. 存在异常,由HandlerExceptionResolver组件处理异常得到新的ModelAndView对象。
  12. 不存在异常,由ViewResolver组件根据ModelAndView对象得到实际的View
  13. 渲染视图
  14. 调用拦截器的afterCompletion方法。

五.在Spring的环境下使用SpringMVC

1.Bean被创建两次?

Spring的IOC容器不应该扫描SpringMVC中的Bean,对应的SpringMVC的IOC容器不应该扫描Spring中的Bean。

2.在SpringMVC配置文件中引用业务层的Bean

  • 多个SpringIOC容器之间可以设置为父子关系,以实现良好的解耦。
  • SpringMVC WEB层容器可作为"业务层"Spring容器的子容器,即WEB层容器可以引用业务层容器的Bean,而业务层容器却访问不到WEB层容器的Bean。

六、Spring和Struts2对比

  1. SpringMVC的入口是Servlet,而Struts2是Filter.
  2. SpringMVC会比Struts2快点,SpringMVC是基于方法设计的,而Struts2是基于类,每次发一个请求都会实实例一个Action
  3. SpringMVC使用更加简洁,开发效率高。
  4. Struts2的OGNL表达式使页面开发效率相比SpringMVC要高一点。

Guess you like

Origin www.cnblogs.com/jack1995/p/10958267.html