Writing in the front

  • All Spring content

Spring – the MVC interceptor

  • interceptor
  • similarServletThe development of theFilterIs used for preprocessing and post-processing of the processor
  • The difference between interceptors and filters

Custom interceptors

  • Create an interceptor class implementationHandleInterceptorinterface
// Get the content of the request before the target method executes, Public Boolean preHandle(HttpServletRequest Request, HttpServletResponse Response, Object handler) throws Exception { System.out.println("preHandle"); String param = request.getParameter("param"); // Return false. if (param.equals("yes")) { return true; } else {/ / forwarding address remains the same request. GetRequestDispatcher (". / error JSP "). The forward (request, response); return false; }} // After the target is sent, Public void postHandle(HttpServletRequest Request, HttpServletResponse Response, Object Handler, ModelAndView ModelAndView) throws Exception {// Obtain the view model ModelAndView to modify modelAndView.addObject("name","lisi"); System.out.println("postHandle"); } // After all the processes have been executed, Public void afterCompletion(HttpServletRequest Request, HttpServletResponse Response, Object Handler, Exception ex) throws Exception { System.out.println("afterCompletion"); }Copy the code
  • Configuring interceptors
<! < MVC :interceptors> < MVC :interceptor> <! - for which resources interception - > < MVC: mapping path = "/ * *" / > < bean class = "com. Java. The interceptor. MyInterceptor1" > < / bean > < / MVC: interceptor > </mvc:interceptors>Copy the code
  • The effect
PreHandle // show... postHandle afterCompletionCopy the code

Configure multiple interceptors

  • configuration
<! < MVC :interceptors> < MVC :interceptor> <! - for which resources interception - > < MVC: mapping path = "/ * *" / > < bean class = "com. Java. The interceptor. MyInterceptor1" > < / bean > < / MVC: interceptor > <mvc:interceptor> <! - for which resources interception - > < MVC: mapping path = "/ * *" / > < bean class = "com. Java. The interceptor. MyInterceptor2" > < / bean > < / MVC: interceptor > </mvc:interceptors>Copy the code
  • The effect
preHandle
preHandle22222
show...
postHandle22222
postHandle
afterCompletion2222222
afterCompletion
Copy the code

Interceptors intercept static resource solutions

  • The web.xml configuration
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>/ass/*"</url-pattern>
    <url-pattern>/img/*</url-pattern>
</servlet-mapping>
Copy the code
  • Spring – web. XML configuration
<! -- MVC static resource --> < MVC :default-servlet-handler/> <! < MVC :interceptors> < MVC :interceptor> < MVC :mapping path="/**"/> <bean class="com.java.interceptor.PrivilegeInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>Copy the code
  • Inceptor excludes blocked access
<! < MVC :exclude-mapping path="/user/login"/>Copy the code

Spring-mvc exception handling mechanism

  • The front controller finds the frame exception handler to handle

Simple exception handler

  • A simple exception handler provided by Spring-MVCSimpleMappingExceptionResolver
  • Spring – the MVC. XML configuration
<! - configuration simple mapping exception handler - > < bean class = "org. Springframework. Web. Servlet. Handler. SimpleMappingExceptionResolver" > <! /> < Property name=" exceptionView "value="error"/> < Property name="exceptionMappings"> <map> <! - the exception class jump interface - > < entry key = "com. Java. Exception. MyException" value = "error_myexception" > < / entry > < entry key="java.lang.ClassCastException" value="error_classCastexception"/> </map> </property> </bean>Copy the code

Custom exception handling

  • Exception handling interface for SpingHandlerExceptionResolverDefine your own exception handler
  • Create a custom exception handler class
Public class MyExceptionResolver implements HandlerExceptionResolver {// parameter exception Exception object public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView modelAndView = new ModelAndView(); If (e instanceof MyException) {modelAndView.addobject ("info"," custom exception "); modelAndView.setViewName("error_myexception"); } else if (e instanceof ClassCastException) { modelAndView.addObject("info","ClassCastException"); modelAndView.setViewName("error_classCastexception"); } modelAndView.addObject("info","otherException"); modelAndView.setViewName("error"); return modelAndView; }}Copy the code
  • Spring – the MVC. XML configuration
<! - custom exception handler - > < bean class = "com. Java. Reslover. MyExceptionResolver" > < / bean >Copy the code