📄 multiactioncontroller.java
字号:
if (logger.isDebugEnabled()) {
logger.debug("Found exception handler method [" + method + "]");
}
}
//---------------------------------------------------------------------
// Implementation of LastModified
//---------------------------------------------------------------------
/**
* Try to find an XXXXLastModified method, where XXXX is the name of a handler.
* Return -1 if there's no such handler, indicating that content must be updated.
* @see org.springframework.web.servlet.mvc.LastModified#getLastModified(HttpServletRequest)
*/
public long getLastModified(HttpServletRequest request) {
try {
String handlerMethodName = this.methodNameResolver.getHandlerMethodName(request);
Method lastModifiedMethod = (Method) this.lastModifiedMethodMap.get(handlerMethodName);
if (lastModifiedMethod != null) {
try {
// Invoke the last-modified method...
Long wrappedLong = (Long) lastModifiedMethod.invoke(this.delegate, new Object[] {request});
return (wrappedLong != null ? wrappedLong.longValue() : -1);
}
catch (Exception ex) {
// We encountered an error invoking the last-modified method.
// We can't do anything useful except log this, as we can't throw an exception.
logger.error("Failed to invoke last-modified method", ex);
}
}
}
catch (NoSuchRequestHandlingMethodException ex) {
// No handler method for this request. This shouldn't happen, as this
// method shouldn't be called unless a previous invocation of this class
// has generated content. Do nothing, that's OK: We'll return default.
}
return -1L;
}
//---------------------------------------------------------------------
// Implementation of AbstractController
//---------------------------------------------------------------------
/**
* Determine a handler method and invoke it.
* @see MethodNameResolver#getHandlerMethodName
* @see #invokeNamedMethod
* @see #handleNoSuchRequestHandlingMethod
*/
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
String methodName = this.methodNameResolver.getHandlerMethodName(request);
return invokeNamedMethod(methodName, request, response);
}
catch (NoSuchRequestHandlingMethodException ex) {
return handleNoSuchRequestHandlingMethod(ex, request, response);
}
}
/**
* Handle the case where no request handler method was found.
* <p>The default implementation logs a warning and sends an HTTP 404 error.
* Alternatively, a fallback view could be chosen, or the
* NoSuchRequestHandlingMethodException could be rethrown as-is.
* @param ex the NoSuchRequestHandlingMethodException to be handled
* @param request current HTTP request
* @param response current HTTP response
* @return a ModelAndView to render, or <code>null</code> if handled directly
* @throws Exception an Exception that should be thrown as result of the servlet request
*/
protected ModelAndView handleNoSuchRequestHandlingMethod(
NoSuchRequestHandlingMethodException ex, HttpServletRequest request, HttpServletResponse response)
throws Exception {
pageNotFoundLogger.warn(ex.getMessage());
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return null;
}
/**
* Invokes the named method.
* <p>Uses a custom exception handler if possible; otherwise, throw an
* unchecked exception; wrap a checked exception or Throwable.
*/
protected final ModelAndView invokeNamedMethod(
String methodName, HttpServletRequest request, HttpServletResponse response) throws Exception {
Method method = (Method) this.handlerMethodMap.get(methodName);
if (method == null) {
throw new NoSuchRequestHandlingMethodException(methodName, getClass());
}
try {
Class[] paramTypes = method.getParameterTypes();
List params = new ArrayList(4);
params.add(request);
params.add(response);
if (paramTypes.length >= 3 && paramTypes[2].equals(HttpSession.class)) {
HttpSession session = request.getSession(false);
if (session == null) {
throw new HttpSessionRequiredException(
"Pre-existing session required for handler method '" + methodName + "'");
}
params.add(session);
}
// If last parameter isn't of HttpSession type, it's a command.
if (paramTypes.length >= 3 &&
!paramTypes[paramTypes.length - 1].equals(HttpSession.class)) {
Object command = newCommandObject(paramTypes[paramTypes.length - 1]);
params.add(command);
bind(request, command);
}
Object returnValue = method.invoke(this.delegate, params.toArray(new Object[params.size()]));
return massageReturnValueIfNecessary(returnValue);
}
catch (InvocationTargetException ex) {
// The handler method threw an exception.
return handleException(request, response, ex.getTargetException());
}
catch (Exception ex) {
// The binding process threw an exception.
return handleException(request, response, ex);
}
}
/**
* Processes the return value of a handler method to ensure that it either returns
* <code>null</code> or an instance of {@link ModelAndView}. When returning a {@link Map},
* the {@link Map} instance is wrapped in a new {@link ModelAndView} instance.
*/
private ModelAndView massageReturnValueIfNecessary(Object returnValue) {
if (returnValue instanceof ModelAndView) {
return (ModelAndView) returnValue;
}
else if (returnValue instanceof Map) {
return new ModelAndView().addAllObjects((Map) returnValue);
}
else {
// Either returned null or was 'void' return.
// We'll assume that the handle method already wrote the response.
return null;
}
}
/**
* Create a new command object of the given class.
* <p>This implementation uses <code>BeanUtils.instantiateClass</code>,
* so commands need to have public no-arg constructors.
* Subclasses can override this implementation if desired.
* @throws Exception if the command object could not be instantiated
* @see org.springframework.beans.BeanUtils#instantiateClass(Class)
*/
protected Object newCommandObject(Class clazz) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Creating new command of class [" + clazz.getName() + "]");
}
return BeanUtils.instantiateClass(clazz);
}
/**
* Bind request parameters onto the given command bean
* @param request request from which parameters will be bound
* @param command command object, that must be a JavaBean
* @throws Exception in case of invalid state or arguments
*/
protected void bind(HttpServletRequest request, Object command) throws Exception {
logger.debug("Binding request parameters onto MultiActionController command");
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
if (this.validators != null) {
for (int i = 0; i < this.validators.length; i++) {
if (this.validators[i].supports(command.getClass())) {
ValidationUtils.invokeValidator(this.validators[i], command, binder.getBindingResult());
}
}
}
binder.closeNoCatch();
}
/**
* Create a new binder instance for the given command and request.
* <p>Called by <code>bind</code>. Can be overridden to plug in custom
* ServletRequestDataBinder subclasses.
* <p>The default implementation creates a standard ServletRequestDataBinder,
* and invokes <code>initBinder</code>. Note that <code>initBinder</code>
* will not be invoked if you override this method!
* @param request current HTTP request
* @param command the command to bind onto
* @return the new binder instance
* @throws Exception in case of invalid state or arguments
* @see #bind
* @see #initBinder
*/
protected ServletRequestDataBinder createBinder(HttpServletRequest request, Object command) throws Exception {
ServletRequestDataBinder binder = new ServletRequestDataBinder(command, getCommandName(command));
initBinder(request, binder);
return binder;
}
/**
* Return the command name to use for the given command object.
* <p>Default is "command".
* @param command the command object
* @return the command name to use
* @see #DEFAULT_COMMAND_NAME
*/
protected String getCommandName(Object command) {
return DEFAULT_COMMAND_NAME;
}
/**
* Initialize the given binder instance, for example with custom editors.
* Called by <code>createBinder</code>.
* <p>This method allows you to register custom editors for certain fields of your
* command class. For instance, you will be able to transform Date objects into a
* String pattern and back, in order to allow your JavaBeans to have Date properties
* and still be able to set and display them in an HTML interface.
* <p>The default implementation is empty.
* <p>Note: the command object is not directly passed to this method, but it's available
* via {@link org.springframework.validation.DataBinder#getTarget()}
* @param request current HTTP request
* @param binder new binder instance
* @throws Exception in case of invalid state or arguments
* @see #createBinder
* @see org.springframework.validation.DataBinder#registerCustomEditor
* @see org.springframework.beans.propertyeditors.CustomDateEditor
*/
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
initBinder((ServletRequest) request, binder);
}
/**
* Initialize the given binder instance, for example with custom editors.
* @deprecated as of Spring 2.0:
* use <code>initBinder(HttpServletRequest, ServletRequestDataBinder)</code> instead
*/
protected void initBinder(ServletRequest request, ServletRequestDataBinder binder) throws Exception {
}
/**
* Determine the exception handler method for the given exception.
* <p>Can return <code>null</code> if not found.
* @return a handler for the given exception type, or <code>null</code>
* @param exception the exception to handle
*/
protected Method getExceptionHandler(Throwable exception) {
Class exceptionClass = exception.getClass();
if (logger.isDebugEnabled()) {
logger.debug("Trying to find handler for exception class [" + exceptionClass.getName() + "]");
}
Method handler = (Method) this.exceptionHandlerMap.get(exceptionClass);
while (handler == null && !exceptionClass.equals(Throwable.class)) {
if (logger.isDebugEnabled()) {
logger.debug("Trying to find handler for exception superclass [" + exceptionClass.getName() + "]");
}
exceptionClass = exceptionClass.getSuperclass();
handler = (Method) this.exceptionHandlerMap.get(exceptionClass);
}
return handler;
}
/**
* We've encountered an exception thrown from a handler method.
* Invoke an appropriate exception handler method, if any.
* @param request current HTTP request
* @param response current HTTP response
* @param ex the exception that got thrown
* @return a ModelAndView to render the response
*/
private ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Throwable ex)
throws Exception {
Method handler = getExceptionHandler(ex);
if (handler != null) {
return invokeExceptionHandler(handler, request, response, ex);
}
// If we get here, there was no custom handler
if (ex instanceof Exception) {
throw (Exception) ex;
}
if (ex instanceof Error) {
throw (Error) ex;
}
// Should never happen!
throw new NestedServletException("Unknown Throwable type encountered", ex);
}
/**
* Invoke the selected exception handler.
* @param handler handler method to invoke
*/
private ModelAndView invokeExceptionHandler(
Method handler, HttpServletRequest request, HttpServletResponse response, Throwable ex)
throws Exception {
// If we get here, we have a handler.
if (logger.isDebugEnabled()) {
logger.debug("Invoking exception handler [" + handler + "] for exception [" + ex + "]");
}
try {
Object returnValue = handler.invoke(this.delegate, new Object[] {request, response, ex});
return massageReturnValueIfNecessary(returnValue);
}
catch (InvocationTargetException ex2) {
Throwable targetEx = ex2.getTargetException();
if (targetEx instanceof Exception) {
throw (Exception) targetEx;
}
if (targetEx instanceof Error) {
throw (Error) targetEx;
}
// Should never happen!
throw new NestedServletException("Unknown Throwable type encountered", targetEx);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -