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 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)

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
 java.lang.String getContextPath()
          Return the context path of the current request, i.e.
 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.
 java.lang.String getMessage(MessageSourceResolvable resolvable, boolean htmlEscape)
          Retrieve the given MessageSourceResolvable (e.g.
 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.
 Theme getTheme()
          Return the current theme.
 WebApplicationContext getWebApplicationContext()
          Return the current WebApplicationContext.
 boolean isDefaultHtmlEscape()
          Default HTML escaping?
 void setDefaultHtmlEscape(boolean defaultHtmlEscape)
          (De)activate default HTML escaping for messages and errors.
 
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)
               throws javax.servlet.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(javax.servlet.http.HttpServletRequest request,
                      java.util.Map model)
               throws javax.servlet.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 java.lang.String getContextPath()
Return the context path of the current request, i.e. the path that indicates the current web application.

See Also:
HttpServletRequest.getContextPath()

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.


isDefaultHtmlEscape

public boolean isDefaultHtmlEscape()
Default HTML escaping?


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

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


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