org.springframework.web.servlet.support
Class RequestContext

java.lang.Object
  extended byorg.springframework.web.servlet.support.RequestContext

public class RequestContext
extends java.lang.Object

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's "useBean" tag, 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.

Since:
03.03.2003
Author:
Juergen Hoeller
See Also:
AbstractView.setRequestContextAttribute(java.lang.String), UrlBasedViewResolver.setRequestContextAttribute(java.lang.String)

Constructor Summary
RequestContext(javax.servlet.http.HttpServletRequest request)
          Create a new RequestContext for the given request, using the request attributes for Errors retrieval.
RequestContext(javax.servlet.http.HttpServletRequest request, java.util.Map model)
          Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
 
Method Summary
 BindStatus getBindStatus(java.lang.String path)
          Create a BindStatus for the given bind object, using the defaultHtmlEscape setting.
 BindStatus getBindStatus(java.lang.String path, boolean htmlEscape)
          Create a BindStatus for the given bind object, using the defaultHtmlEscape setting.
 java.lang.String getContextPath()
          Return the context path of the current request, i.e. the path that indicates the current web application.
 Errors getErrors(java.lang.String name)
          Retrieve the Errors instance for the given bind object, using the defaultHtmlEscape setting.
 Errors getErrors(java.lang.String name, boolean htmlEscape)
          Retrieve the Errors instance for the given bind object.
 java.util.Locale getLocale()
          Return the current locale.
 java.lang.String getMessage(MessageSourceResolvable resolvable)
          Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance), using the defaultHtmlEscape setting.
 java.lang.String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape)
          Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance).
 java.lang.String getMessage(java.lang.String code)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, boolean htmlEscape)
          Retrieve the message for the given code.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, boolean htmlEscape)
          Retrieve the message for the given code.
 java.lang.String getMessage(java.lang.String code, java.lang.String defaultMessage)
          Retrieve the message for the given code, using the defaultHtmlEscape setting.
protected  javax.servlet.http.HttpServletRequest getRequest()
          Return the underlying HttpServletRequest.
 java.lang.String getRequestUri()
          Return the request URI of the current request, i.e. the invoked URL without parameters.
 Theme getTheme()
          Return the current theme.
 java.lang.String getThemeMessage(MessageSourceResolvable resolvable)
          Retrieve the given MessageSourceResolvable in the current theme.
 java.lang.String getThemeMessage(java.lang.String code)
          Retrieve the theme message for the given code.
 java.lang.String getThemeMessage(java.lang.String code, java.lang.String defaultMessage)
          Retrieve the theme message for the given code.
 java.lang.String getThemeMessage(java.lang.String code, java.lang.String[] args)
          Retrieve the theme message for the given code.
 java.lang.String getThemeMessage(java.lang.String code, java.lang.String[] args, java.lang.String defaultMessage)
          Retrieve the theme message for the given code.
 UrlPathHelper getUrlPathHelper()
          Return the UrlPathHelper used for context path and request URI decoding.
 WebApplicationContext getWebApplicationContext()
          Return the current WebApplicationContext.
 boolean isDefaultHtmlEscape()
          Is default HTML escaping active?
 void setDefaultHtmlEscape(boolean defaultHtmlEscape)
          (De)activate default HTML escaping for messages and errors, for the scope of this RequestContext.
 void setUrlPathHelper(UrlPathHelper urlPathHelper)
          Set the UrlPathHelper to use for context path and request URI decoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RequestContext

public RequestContext(javax.servlet.http.HttpServletRequest request)
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(javax.servlet.http.HttpServletRequest request,
                      java.util.Map model)
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

getRequest

protected javax.servlet.http.HttpServletRequest getRequest()
Return the underlying HttpServletRequest. Only intended for cooperating classes in this package.


getWebApplicationContext

public WebApplicationContext getWebApplicationContext()
Return the current WebApplicationContext.


getLocale

public java.util.Locale getLocale()
Return the current locale.


getTheme

public Theme getTheme()
Return the current theme.


setDefaultHtmlEscape

public void setDefaultHtmlEscape(boolean defaultHtmlEscape)
(De)activate default HTML escaping for messages and errors, for the scope of this RequestContext. The default is the application-wide setting (the "defaultHtmlEscape" context-param in web.xml).

See Also:
WebUtils.isDefaultHtmlEscape(javax.servlet.ServletContext)

isDefaultHtmlEscape

public boolean isDefaultHtmlEscape()
Is default HTML escaping active?


setUrlPathHelper

public void setUrlPathHelper(UrlPathHelper urlPathHelper)
Set the UrlPathHelper to use for context path and request URI decoding. Can be used to pass a shared UrlPathHelper instance in.

A default UrlPathHelper is always available.


getUrlPathHelper

public UrlPathHelper getUrlPathHelper()
Return the UrlPathHelper used for context path and request URI decoding. Can be used to configure the current UrlPathHelper.

A default UrlPathHelper is always available.


getContextPath

public java.lang.String getContextPath()
Return the context path of the current request, i.e. the path that indicates the current web application.

Delegates to the UrlPathHelper for decoding.

See Also:
HttpServletRequest.getContextPath(), getUrlPathHelper()

getRequestUri

public java.lang.String getRequestUri()
Return the request URI of the current request, i.e. the invoked URL without parameters. This is particularly useful as HTML form action target.

Note that you should create your RequestContext instance before forwarding to your JSP view, if you intend to determine the request URI! The optimal way to do so is to set "requestContextAttribute" on your view. Else, you'll get the URI of your JSP rather than the one of your controller.

Side note: As alternative for an HTML form action, either specify an empty "action" or omit the "action" attribute completely. This is not covered by the HTML spec, though, but known to work reliably on all current browsers.

Delegates to the UrlPathHelper for decoding.

See Also:
HttpServletRequest.getRequestURI(), getUrlPathHelper(), AbstractView.setRequestContextAttribute(java.lang.String), UrlBasedViewResolver.setRequestContextAttribute(java.lang.String)

getMessage

public java.lang.String getMessage(java.lang.String code,
                                   java.lang.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 java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   java.lang.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 java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   java.lang.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 java.lang.String getMessage(java.lang.String code)
                            throws 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 java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args)
                            throws 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 java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   boolean htmlEscape)
                            throws 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 java.lang.String getMessage(MessageSourceResolvable resolvable)
                            throws 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 java.lang.String getMessage(MessageSourceResolvable resolvable,
                                   boolean htmlEscape)
                            throws 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

getThemeMessage

public java.lang.String getThemeMessage(java.lang.String code,
                                        java.lang.String defaultMessage)
Retrieve the theme message for the given code.

Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.

Parameters:
code - code of the message
defaultMessage - String to return if the lookup fails
Returns:
the message

getThemeMessage

public java.lang.String getThemeMessage(java.lang.String code,
                                        java.lang.String[] args,
                                        java.lang.String defaultMessage)
Retrieve the theme message for the given code.

Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.

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

getThemeMessage

public java.lang.String getThemeMessage(java.lang.String code)
                                 throws NoSuchMessageException
Retrieve the theme message for the given code.

Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.

Parameters:
code - code of the message
Returns:
the message
Throws:
NoSuchMessageException - if not found

getThemeMessage

public java.lang.String getThemeMessage(java.lang.String code,
                                        java.lang.String[] args)
                                 throws NoSuchMessageException
Retrieve the theme message for the given code.

Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.

Parameters:
code - code of the message
args - arguments for the message, or null if none
Returns:
the message
Throws:
NoSuchMessageException - if not found

getThemeMessage

public java.lang.String getThemeMessage(MessageSourceResolvable resolvable)
                                 throws NoSuchMessageException
Retrieve the given MessageSourceResolvable in the current theme.

Note that theme messages are never HTML-escaped, as they typically denote theme-specific resource paths and not client-visible messages.

Parameters:
resolvable - the MessageSourceResolvable
Returns:
the message
Throws:
NoSuchMessageException - if not found

getErrors

public Errors getErrors(java.lang.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(java.lang.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

getBindStatus

public BindStatus getBindStatus(java.lang.String path)
                         throws java.lang.IllegalStateException
Create a BindStatus for the given bind object, using the defaultHtmlEscape setting.

Parameters:
path - the bean and property path for which values and errors will be resolved (e.g. "person.age")
Returns:
the new BindStatus instance
Throws:
java.lang.IllegalStateException - if no corresponding Errors object found

getBindStatus

public BindStatus getBindStatus(java.lang.String path,
                                boolean htmlEscape)
                         throws java.lang.IllegalStateException
Create a BindStatus for the given bind object, using the defaultHtmlEscape setting.

Parameters:
path - the bean and property path for which values and errors will be resolved (e.g. "person.age")
htmlEscape - create a BindStatus with automatic HTML escaping?
Returns:
the new BindStatus instance
Throws:
java.lang.IllegalStateException - if no corresponding Errors object found


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