Springmvc notes (3)

Time:2022-6-7

1、 Springmvc interceptor

1.1 quick start

step

  1. 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...");
        }
    }
  2. Configuring Interceptors

  3. Test the interception effect of interceptor

    preHandle…
    Object function execution..
    postHandle..
    afterCom…

1.2 user login permission control

step

  1. 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;
        }
    }
  2. 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.

SystematicDaoServiceControllerAll 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

  1. 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;
    }
}
  1. to configure

Recommended Today

ffmpeg pushes video with RTP protocol

docker build environment docker pull abdulachik/ffmpeg.js:latest docker run -it -p 8080:8080 -v /Users/workspace/Downloads/ffmpeg_wasm:/tmp –privileged=true abdulachik/ffmpeg.js:latest /bin/bash cd /tmp Process overview camera->ffmpeg->encode to h264 video->pass to h264 decoder->play(You can also push the video directly to RTMP protocol after encoding, and the local decoding and playback is only for testing) ffmpeg encodes video stream (from camera) to […]