Package org.springframework.web.servlet.mvc

# Class AbstractController

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.support.WebContentGenerator
org.springframework.web.servlet.mvc.AbstractController

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.context.ApplicationContextAware`, `org.springframework.web.context.ServletContextAware`, `Controller`

Direct Known Subclasses:
  `AbstractUrlViewController`, `ParameterizableViewController`, `ServletForwardingController`, `ServletWrappingController`

public abstract class AbstractController extends WebContentGenerator implements Controller
Convenient superclass for controller implementations, using the Template Method design pattern.

Workflow (and that defined by interface):

1. `handleRequest()` will be called by the DispatcherServlet
2. Inspection of supported methods (ServletException if request method is not support)
3. If session is required, try to get it (ServletException if not found)
4. Set caching headers if needed according to the cacheSeconds property
5. Call abstract method `handleRequestInternal()` (optionally synchronizing around the call on the HttpSession), which should be implemented by extending classes to provide actual functionality to return `ModelAndView` objects.

Exposed configuration properties (and those defined by interface):

name | default | description
supportedMethods | GET,POST | comma-separated (CSV) list of methods supported by this controller, such as GET, POST and PUT
requireSession | false | whether a session should be required for requests to be able to be handled by this controller. This ensures that derived controller can - without fear of null pointers - call request.getSession() to retrieve a session. If no session can be found while processing the request, a ServletException will be thrown
cacheSeconds | -1 | indicates the amount of seconds to include in the cache header for the response following on this request. 0 (zero) will include headers for no caching at all, -1 (the default) will not generate any headers and any positive number will generate headers that state the amount indicated as seconds to cache the content
synchronizeOnSession | false | whether the call to `handleRequestInternal` should be synchronized around the HttpSession, to serialize invocations from the same client. No effect if there is no HttpSession.

Author:
  Rod Johnson, Juergen Hoeller, Rossen Stoyanchev
See Also:
  - `WebContentInterceptor`

## Field Summary

### Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator

`HEADER_CACHE_CONTROL, METHOD_GET, METHOD_HEAD, METHOD_POST`

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

## Constructor Summary

Constructors
Constructor
Description
`AbstractController()`
Create a new AbstractController which supports HTTP methods GET, HEAD and POST by default.
`AbstractController(boolean restrictDefaultSupportedMethods)`
Create a new AbstractController.

## Method Summary

Modifier and Type
Method
Description
`ModelAndView`
`handleRequest(HttpServletRequest request, HttpServletResponse response)`
Process the request and return a ModelAndView object which the DispatcherServlet will render.
`protected abstract ModelAndView`
`handleRequestInternal(HttpServletRequest request, HttpServletResponse response)`
Template method.
`final boolean`
`isSynchronizeOnSession()`
Return whether controller execution should be synchronized on the session.
`final void`
`setSynchronizeOnSession(boolean synchronizeOnSession)`
Set if controller execution should be synchronized on the session, to serialize parallel invocations from the same client.

### Methods inherited from class org.springframework.web.servlet.support.WebContentGenerator

`applyCacheControl, applyCacheSeconds, checkAndPrepare, checkRequest, getAllowHeader, getCacheControl, getCacheSeconds, getSupportedMethods, getVaryByRequestHeaders, isRequireSession, prepareResponse, setCacheControl, setCacheSeconds, setRequireSession, setSupportedMethods, setVaryByRequestHeaders`

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### AbstractController

public AbstractController()
Create a new AbstractController which supports HTTP methods GET, HEAD and POST by default.

### AbstractController

public AbstractController(boolean restrictDefaultSupportedMethods)
Create a new AbstractController.

Parameters:
  `restrictDefaultSupportedMethods` - `true` if this controller should support HTTP methods GET, HEAD and POST by default, or `false` if it should be unrestricted
Since:
  4.3

## Method Details

### setSynchronizeOnSession

public final void setSynchronizeOnSession(boolean synchronizeOnSession)
Set if controller execution should be synchronized on the session, to serialize parallel invocations from the same client.

More specifically, the execution of the `handleRequestInternal` method will get synchronized if this flag is "true". The best available session mutex will be used for the synchronization; ideally, this will be a mutex exposed by HttpSessionMutexListener.

The session mutex is guaranteed to be the same object during the entire lifetime of the session, available under the key defined by the `SESSION_MUTEX_ATTRIBUTE` constant. It serves as a safe reference to synchronize on for locking on the current session.

In many cases, the HttpSession reference itself is a safe mutex as well, since it will always be the same object reference for the same active logical session. However, this is not guaranteed across different servlet containers; the only 100% safe way is a session mutex.

See Also:
  - `handleRequestInternal(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`
  - `HttpSessionMutexListener`
  - `WebUtils.getSessionMutex(jakarta.servlet.http.HttpSession)`

### isSynchronizeOnSession

public final boolean isSynchronizeOnSession()
Return whether controller execution should be synchronized on the session.

### handleRequest

@Nullable public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
Description copied from interface: `Controller`
Process the request and return a ModelAndView object which the DispatcherServlet will render. A `null` return value is not an error: it indicates that this object completed request processing itself and that there is therefore no ModelAndView to render.

Specified by:
  `handleRequest` in interface `Controller`
Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
Returns:
  a ModelAndView to render, or `null` if handled directly
Throws:
  `Exception` - in case of errors

### handleRequestInternal

@Nullable protected abstract ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
Template method. Subclasses must implement this. The contract is the same as for `handleRequest`.

Throws:
  `Exception`
See Also:
  - `handleRequest(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)`