1、 Springmvc interceptor
1.1 quick start
step
-
Create an interceptor class to implement the handlerinterceptor interface
public class MyInterceptor01 implements HandlerInterceptor { /** *Before the target method is executed * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle..."); //True to release return true; } /** *After the target method executes, before the view object returns * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle.."); } /** *Execute after all processes are executed * @param request * @param response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCom..."); } }
-
Configuring Interceptors
-
Test the interception effect of interceptor
preHandle…
Object function execution..
postHandle..
afterCom…
1.2 user login permission control
step
-
Write login authentication interceptor
/** *Login blocking *@author gaoyubo */ public class PrivilegeInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //Judge whether the user logs in //Determine whether the user object exists in the session HttpSession session = request.getSession(); //Get user object User user = (User) session.getAttribute("user"); //Judge whether it exists if(user == null){ //No user //Redirect to login interface response.sendRedirect(request.getContextPath()+"/login.jsp"); return false; } //Release return true; } }
-
Configure interceptors (in spring-mvc.xml)
2、 Springmvc exception handling
2.1 ideas for exception handling
There are two types of exceptions in the system:Expected exceptionandRuntime exception * * * *runtimeexceptionThe former obtains exception information by capturing exceptions, while the latter mainly reduces the occurrence of runtime exceptions by standardizing code development and testing.
SystematicDao、Service、ControllerAll occurrences are thrown upward through throws exception. Finally, the springmvc front-end controller hands it over to the exception handler for exception handling, as shown in the following figure:
2.2 two methods of exception handling
2.2.1 simple exception handler:
Using theSimpleMappingExceptionResolver
1. configure exception handler
2. write exception page
2.2.2 exception handling interface:
Implement spring’sHandelerExceptionResolver
- Implement exception handling interface
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
//E whether the exception is an instance of ClassCastException
if(e instanceof ClassCastException){
//Set up models and views
modelAndView. AddObject ("error", "class conversion exception");
}
else if (e instanceof DataAccessException){
//Set up models and views
modelAndView. AddObject ("error", "data access exception");
}
modelAndView.setViewName("error.jsp");
return modelAndView;
}
}
- to configure