org.springframework.web.servlet.mvc.multiaction
Class MultiActionController

java.lang.Object
  extended byorg.springframework.context.support.ApplicationObjectSupport
      extended byorg.springframework.web.context.support.WebApplicationObjectSupport
          extended byorg.springframework.web.servlet.support.WebContentGenerator
              extended byorg.springframework.web.servlet.mvc.AbstractController
                  extended byorg.springframework.web.servlet.mvc.multiaction.MultiActionController
All Implemented Interfaces:
ApplicationContextAware, Controller, LastModified

public class MultiActionController
extends AbstractController
implements LastModified

Controller implementation that allows multiple request types to be handled by the same class. Subclasses of this class can handle several different types of request with methods of the form

 ModelAndView actionName(HttpServletRequest request, HttpServletResponse response);
May take a third parameter HttpSession in which an existing session will be required, or a third parameter of an arbitrary class that gets treated as command (i.e. an instance of the class gets created, and request parameters get bound to it)

These methods can throw any kind of exception, but should only let propagate those that they consider fatal, or which their class or superclass is prepared to catch by implementing an exception handler.

This model allows for rapid coding, but loses the advantage of compile-time checking. It is similar to a Struts 1.1 DispatchAction, but more sophisticated. Also supports delegation to another object.

An implementation of the MethodNameResolver interface defined in this package should return a method name for a given request, based on any aspect of the request, such as its URL or an "action" parameter. The actual strategy can be configured via the "methodNameResolver" bean property, for each MultiActionController.

The default MethodNameResolver is InternalPathMethodNameResolver; further included strategies are PropertiesMethodNameResolver and ParameterMethodNameResolver.

Subclasses can implement custom exception handler methods with names such as:

 ModelAndView anyMeaningfulName(HttpServletRequest request, HttpServletResponse response, ExceptionClass exception);
The third parameter can be any subclass or Exception or RuntimeException.

There can also be an optional lastModified method for handlers, of signature:

 long anyMeaningfulNameLastModified(HttpServletRequest request)
If such a method is present, it will be invoked. Default return from getLastModified is -1, meaning that the content must always be regenerated.

Note that method overloading isn't allowed.

Author:
Rod Johnson, Juergen Hoeller
See Also:
MethodNameResolver, InternalPathMethodNameResolver, PropertiesMethodNameResolver, ParameterMethodNameResolver, LastModified.getLastModified(javax.servlet.http.HttpServletRequest)

Field Summary
static String LAST_MODIFIED_METHOD_SUFFIX
          Suffix for last-modified methods
static String PAGE_NOT_FOUND_LOG_CATEGORY
          Log category to use when no mapped handler is found for a request.
protected static Log pageNotFoundLogger
          Additional logger to use when no mapped handler is found for a request.
 
Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator
HEADER_CACHE_CONTROL, HEADER_EXPIRES, HEADER_PRAGMA, METHOD_GET, METHOD_HEAD, METHOD_POST
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
MultiActionController()
          Constructor for MultiActionController that looks for handler methods in the present subclass.Caches methods for quick invocation later.
MultiActionController(Object delegate)
          Constructor for MultiActionController that looks for handler methods in delegate, rather than a subclass of this class.
 
Method Summary
protected  void bind(ServletRequest request, Object command)
          Bind request parameters onto the given command bean
protected  Method getExceptionHandler(Throwable exception)
          Can return null if not found.
 long getLastModified(HttpServletRequest request)
          Try to find an XXXXLastModified method, where XXXX is the name of a handler.
 MethodNameResolver getMethodNameResolver()
          Get the MethodNameResolver used by this class.
protected  ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
          Template method.
protected  ModelAndView invokeNamedMethod(String method, HttpServletRequest request, HttpServletResponse response)
          Invoke the named method.
protected  Object newCommandObject(Class clazz)
          Create a new command object of the given class.
 void setDelegate(Object delegate)
          Set the delegate used by this class.
 void setMethodNameResolver(MethodNameResolver methodNameResolver)
          Set the method name resolver that this class should use.
 
Methods inherited from class org.springframework.web.servlet.mvc.AbstractController
handleRequest, isSynchronizeOnSession, setSynchronizeOnSession
 
Methods inherited from class org.springframework.web.servlet.support.WebContentGenerator
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isRequireSession, isUseCacheControlHeader, isUseExpiresHeader, preventCaching, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseExpiresHeader
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, isContextRequired
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LAST_MODIFIED_METHOD_SUFFIX

public static final String LAST_MODIFIED_METHOD_SUFFIX
Suffix for last-modified methods

See Also:
Constant Field Values

PAGE_NOT_FOUND_LOG_CATEGORY

public static final String PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.

See Also:
Constant Field Values

pageNotFoundLogger

protected static final Log pageNotFoundLogger
Additional logger to use when no mapped handler is found for a request.

Constructor Detail

MultiActionController

public MultiActionController()
                      throws ApplicationContextException
Constructor for MultiActionController that looks for handler methods in the present subclass.Caches methods for quick invocation later. This class's use of reflection will impose little overhead at runtime.

Throws:
ApplicationContextException - if the class doesn't contain any action handler methods (and so could never handle any requests).

MultiActionController

public MultiActionController(Object delegate)
                      throws ApplicationContextException
Constructor for MultiActionController that looks for handler methods in delegate, rather than a subclass of this class. Caches methods.

Parameters:
delegate - handler class. This doesn't need to implement any particular interface, as everything is done using reflection.
Throws:
ApplicationContextException - if the class doesn't contain any handler methods
Method Detail

setMethodNameResolver

public final void setMethodNameResolver(MethodNameResolver methodNameResolver)
Set the method name resolver that this class should use. Allows parameterization of handler method mappings.


getMethodNameResolver

public final MethodNameResolver getMethodNameResolver()
Get the MethodNameResolver used by this class.


setDelegate

public final void setDelegate(Object delegate)
                       throws ApplicationContextException
Set the delegate used by this class. The default is "this", assuming that handler methods have been added by a subclass. This method is rarely invoked once the class is configured.

Parameters:
delegate - class containing methods, which may be the present class, the handler methods being in a subclass
Throws:
ApplicationContextException - if there aren't any valid request handling methods in the subclass.

getLastModified

public final long getLastModified(HttpServletRequest request)
Try to find an XXXXLastModified method, where XXXX is the name of a handler. Return -1, indicating that content must be updated, if there's no such handler.

Specified by:
getLastModified in interface LastModified
Parameters:
request - current HTTP request
Returns:
the time the underlying resource was last modified, or -1 meaning that the content must always be regenerated
See Also:
LastModified.getLastModified(HttpServletRequest)

handleRequestInternal

protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                             HttpServletResponse response)
                                      throws Exception
Description copied from class: AbstractController
Template method. Subclasses must implement this. The contract is the same as for handleRequest.

Specified by:
handleRequestInternal in class AbstractController
Throws:
Exception
See Also:
AbstractController.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

invokeNamedMethod

protected final ModelAndView invokeNamedMethod(String method,
                                               HttpServletRequest request,
                                               HttpServletResponse response)
                                        throws Exception
Invoke the named method. Use a custom exception handler if possible; otherwise, throw an unchecked exception; wrap a checked exception or Throwable

Throws:
Exception

newCommandObject

protected Object newCommandObject(Class clazz)
                           throws ServletException
Create a new command object of the given class. Subclasses can override this implementation if they want. This implementation uses class.newInstance(), so commands need to have public no arg constructors.

Throws:
ServletException

bind

protected void bind(ServletRequest request,
                    Object command)
             throws ServletException
Bind request parameters onto the given command bean

Parameters:
request - request from which parameters will be bound
command - command object, that must be a JavaBean
Throws:
ServletException

getExceptionHandler

protected Method getExceptionHandler(Throwable exception)
Can return null if not found.

Parameters:
exception - Won't be a ServletException or IOException
Returns:
a handler for the given exception type


Copyright (C) 2003-2004 The Spring Framework Project.