Package org.springframework.web.servlet.handler

Source Code of org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver

/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;

/**
* Abstract base class for {@link org.springframework.web.servlet.HandlerExceptionResolver HandlerExceptionResolver}
* implementations that support handling exceptions from {@link HandlerMethod}s rather than handlers.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class AbstractHandlerMethodExceptionResolver extends AbstractHandlerExceptionResolver {

  /**
   * Checks if the handler is a {@link HandlerMethod} instance and performs the check against the bean
   * instance it contains. If the provided handler is not an instance of {@link HandlerMethod},
   * {@code false} is returned instead.
   */
  @Override
  protected boolean shouldApplyTo(HttpServletRequest request, Object handler) {
    if (handler == null) {
      return super.shouldApplyTo(request, handler);
    }
    else if (handler instanceof HandlerMethod) {
      HandlerMethod handlerMethod = (HandlerMethod) handler;
      handler = handlerMethod.getBean();
      return super.shouldApplyTo(request, handler);
    }
    else {
      return false;
    }
  }
 
  @Override
  protected final ModelAndView doResolveException(HttpServletRequest request,
                          HttpServletResponse response,
                          Object handler,
                          Exception ex) {
    return doResolveHandlerMethodException(request, response, (HandlerMethod) handler, ex);
  }
 
  /**
   * Actually resolve the given exception that got thrown during on handler execution,
   * returning a ModelAndView that represents a specific error page if appropriate.
   * <p>May be overridden in subclasses, in order to apply specific exception checks.
   * Note that this template method will be invoked <i>after</i> checking whether this
   * resolved applies ("mappedHandlers" etc), so an implementation may simply proceed
   * with its actual exception handling.
   * @param request current HTTP request
   * @param response current HTTP response
   * @param handlerMethod the executed handler method, or <code>null</code> if none chosen at the time
   * of the exception (for example, if multipart resolution failed)
   * @param ex the exception that got thrown during handler execution
   * @return a corresponding ModelAndView to forward to, or <code>null</code> for default processing
   */
  protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
                               HttpServletResponse response,
                               HandlerMethod handlerMethod,
                               Exception ex) {
    return null;
  }
 
}
TOP

Related Classes of org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.