org.springframework.web.servlet.support
Class RequestContext


public class RequestContext

Context holder for request-specific state, like current web application context, current locale, current theme, and potential binding errors. Provides easy access to localized messages and Errors instances.

Suitable for exposition to views, and usage within , JSP scriptlets, JSTL EL, Velocity templates, etc. Necessary for views that do not have access to the servlet request, like Velocity templates.

Can be instantiated manually, or automatically exposed to views as model attribute via AbstractView's requestContextAttribute property.

Author:
Juergen Hoeller
Since: 03.03.2003
See Also: org.springframework.web.servlet.view.AbstractView.setRequestContextAttribute(java.lang.String)

Constructor Summary
RequestContext(HttpServletRequest request)
          Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
RequestContext(HttpServletRequest request, Map model)
          Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.

Method Summary
 StringgetContextPath()
          Return the context path of the current request, i.e.
 ErrorsgetErrors(String name)
          Retrieve the Errors instance for the given bind object, using the defaultHtmlEscape setting.
 ErrorsgetErrors(String name, boolean htmlEscape)
          Retrieve the Errors instance for the given bind object.
 LocalegetLocale()
          Return the current locale.
 StringgetMessage(String code, String defaultMessage)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 StringgetMessage(String code, Object[] args, String defaultMessage)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 StringgetMessage(String code, Object[] args, String defaultMessage, boolean htmlEscape)
          Retrieve the message for the given code.
 StringgetMessage(String code)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 StringgetMessage(String code, Object[] args)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 StringgetMessage(String code, Object[] args, boolean htmlEscape)
          Retrieve the message for the given code.
 StringgetMessage(MessageSourceResolvable resolvable)
          Retrieve the given MessageSourceResolvable (e.g.
 StringgetMessage(MessageSourceResolvable resolvable, boolean htmlEscape)
          Retrieve the given MessageSourceResolvable (e.g.
 ThemegetTheme()
          Return the current theme.
 WebApplicationContextgetWebApplicationContext()
          Return the current WebApplicationContext.
 booleanisDefaultHtmlEscape()
          Default HTML escaping?
 voidsetDefaultHtmlEscape(boolean defaultHtmlEscape)
          (De)activate default HTML escaping for messages and errors.

Constructor Detail

RequestContext

public RequestContext(HttpServletRequest request)
throws ServletException
Create a new RequestContext for the given request, using the request attributes for Errors retrieval.

This only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as request attributes. It will typically be used within JSPs or custom tags.

Parameters:
request - current HTTP request

RequestContext

public RequestContext(HttpServletRequest request, Map model)
throws ServletException
Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.

This works with all View implementations. It will typically be used by View implementations.

Parameters:
request - current HTTP request
model - the model attributes for the current view
Method Detail

getContextPath

public String getContextPath()
Return the context path of the current request, i.e. the path that indicates the current web application.
See Also:
javax.servlet.http.HttpServletRequest#getContextPath

getErrors

public Errors getErrors(String name)
Retrieve the Errors instance for the given bind object, using the defaultHtmlEscape setting.
Parameters:
name - name of the bind object
Returns: the Errors instance, or null if not found

getErrors

public Errors getErrors(String name, boolean htmlEscape)
Retrieve the Errors instance for the given bind object.
Parameters:
name - name of the bind object
htmlEscape - create an Errors instance with automatic HTML escaping?
Returns: the Errors instance, or null if not found

getLocale

public Locale getLocale()
Return the current locale.

getMessage

public String getMessage(String code, String defaultMessage)
Retrieve the message for the given code, using the defaultHtmlEscape setting.
Parameters:
code - code of the message
defaultMessage - String to return if the lookup fails
Returns: the message

getMessage

public String getMessage(String code, Object[] args, String defaultMessage)
Retrieve the message for the given code, using the defaultHtmlEscape setting.
Parameters:
code - code of the message
args - arguments for the message, or null if none
defaultMessage - String to return if the lookup fails
Returns: the message

getMessage

public String getMessage(String code, Object[] args, String defaultMessage, boolean htmlEscape)
Retrieve the message for the given code.
Parameters:
code - code of the message
args - arguments for the message, or null if none
defaultMessage - String to return if the lookup fails
htmlEscape - HTML escape the message?
Returns: the message

getMessage

public String getMessage(String code)
throws org.springframework.context.NoSuchMessageException
Retrieve the message for the given code, using the defaultHtmlEscape setting.
Parameters:
code - code of the message
Returns: the message
Throws:
NoSuchMessageException - if not found

getMessage

public String getMessage(String code, Object[] args)
throws org.springframework.context.NoSuchMessageException
Retrieve the message for the given code, using the defaultHtmlEscape setting.
Parameters:
code - code of the message
args - arguments for the message, or null if none
Returns: the message
Throws:
NoSuchMessageException - if not found

getMessage

public String getMessage(String code, Object[] args, boolean htmlEscape)
throws org.springframework.context.NoSuchMessageException
Retrieve the message for the given code.
Parameters:
code - code of the message
args - arguments for the message, or null if none
htmlEscape - HTML escape the message?
Returns: the message
Throws:
NoSuchMessageException - if not found

getMessage

public String getMessage(MessageSourceResolvable resolvable)
throws org.springframework.context.NoSuchMessageException
Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the defaultHtmlEscape setting.
Parameters:
resolvable - the MessageSourceResolvable
Returns: the message
Throws:
NoSuchMessageException - if not found

getMessage

public String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape)
throws org.springframework.context.NoSuchMessageException
Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).
Parameters:
resolvable - the MessageSourceResolvable
htmlEscape - HTML escape the message?
Returns: the message
Throws:
NoSuchMessageException - if not found

getTheme

public Theme getTheme()
Return the current theme.

getWebApplicationContext

public WebApplicationContext getWebApplicationContext()
Return the current WebApplicationContext.

isDefaultHtmlEscape

public boolean isDefaultHtmlEscape()
Default HTML escaping?

setDefaultHtmlEscape

public void setDefaultHtmlEscape(boolean defaultHtmlEscape)
(De)activate default HTML escaping for messages and errors.

Association Links

to Class java.util.Map

to Class org.springframework.web.context.WebApplicationContext

to Class java.util.Locale

to Class org.springframework.ui.context.Theme

to Class java.util.Map