springmvc3个适配器

发布时间:2019-07-04 10:04:51编辑:auto阅读(1357)

    SimpleControllerHandlerAdapter

    public class SimpleControllerHandlerAdapter implements HandlerAdapter {
        @Override
        public boolean supports(Object handler) {
            return (handler instanceof Controller);
        }
        @Override
        public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            return ((Controller) handler).handleRequest(request, response);
        }
        @Override
        public long getLastModified(HttpServletRequest request, Object handler) {
            if (handler instanceof LastModified) {
                return ((LastModified) handler).getLastModified(request);
            }
            return -1L;
        }
    }
    public interface Controller {
        /**
         * Process the request and return a ModelAndView object which the DispatcherServlet
         * will render. A {@code null} return value is not an error: It indicates that
         * this object completed request processing itself, thus there is no ModelAndView
         * to render.
         * @param request current HTTP request
         * @param response current HTTP response
         * @return a ModelAndView to render, or {@code null} if handled directly
         * @throws Exception in case of errors
         */
        ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
    }

    HttpRequestHandlerAdapter

    public class HttpRequestHandlerAdapter implements HandlerAdapter {
        @Override
        public boolean supports(Object handler) {
            return (handler instanceof HttpRequestHandler);
        }
        @Override
        public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            ((HttpRequestHandler) handler).handleRequest(request, response);
            return null;
        }
        @Override
        public long getLastModified(HttpServletRequest request, Object handler) {
            if (handler instanceof LastModified) {
                return ((LastModified) handler).getLastModified(request);
            }
            return -1L;
        }
    }
    public interface HttpRequestHandler {
        /**
         * Process the given request, generating a response.
         * @param request current HTTP request
         * @param response current HTTP response
         * @throws ServletException in case of general errors
         * @throws IOException in case of I/O errors
         */
        void handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException;
    }

    RequestMappingHandlerAdapter

    public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware,
            InitializingBean {
            @Override
        public final boolean supports(Object handler) {
            return handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler);
        }
    @Override
        protected final ModelAndView handleInternal(HttpServletRequest request,
                HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
            if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
                // Always prevent caching in case of session attribute management.
                checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true);
            }
            else {
                // Uses configured default cacheSeconds setting.
                checkAndPrepare(request, response, true);
            }
            // Execute invokeHandlerMethod in synchronized block if required.
            if (this.synchronizeOnSession) {
                HttpSession session = request.getSession(false);
                if (session != null) {
                    Object mutex = WebUtils.getSessionMutex(session);
                    synchronized (mutex) {
                        return invokeHandleMethod(request, response, handlerMethod);
                    }
                }
            }
            return invokeHandleMethod(request, response, handlerMethod);
        }
    /**
         * This implementation always returns -1. An {@code @RequestMapping}
         * method can calculate the lastModified value, call
         * {@link WebRequest#checkNotModified(long)}, and return {@code null}
         * if the result of that call is {@code true}.
         */
        @Override
        protected long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod) {
            return -1;
        }
    }
    public class HandlerMethod {
        private final Object bean;
        private final Method method;
    }


关键字