类 RequestContext

  • 直接已知子类:
    JspAwareRequestContext

    public class RequestContext
    extends 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, etc. Necessary for views that do not have access to the servlet request, like FreeMarker templates.

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

    Will also work outside of DispatcherServlet requests, accessing the root WebApplicationContext and using an appropriate fallback for the locale (the HttpServletRequest's primary locale).

    从以下版本开始:
    03.03.2003
    作者:
    Juergen Hoeller, Rossen Stoyanchev
    另请参阅:
    DispatcherServlet, AbstractView.setRequestContextAttribute(java.lang.String), UrlBasedViewResolver.setRequestContextAttribute(java.lang.String)
    • 字段详细资料

      • WEB_APPLICATION_CONTEXT_ATTRIBUTE

        public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE
        Request attribute to hold the current web application context for RequestContext usage. By default, the DispatcherServlet's context (or the root context as fallback) is exposed.
    • 构造器详细资料

      • RequestContext

        public RequestContext​(HttpServletRequest request,
                              ServletContext servletContext)
        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.

        If a ServletContext is specified, the RequestContext will also work with the root WebApplicationContext (outside a DispatcherServlet).

        参数:
        request - current HTTP request
        servletContext - the servlet context of the web application (can be null; necessary for fallback to root WebApplicationContext)
        另请参阅:
        WebApplicationContext, DispatcherServlet
      • RequestContext

        public RequestContext​(HttpServletRequest request,
                              HttpServletResponse response,
                              ServletContext servletContext,
                              Map<String,​Object> 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.

        If a ServletContext is specified, the RequestContext will also work with a root WebApplicationContext (outside a DispatcherServlet).

        参数:
        request - current HTTP request
        response - current HTTP response
        servletContext - the servlet context of the web application (can be null; necessary for fallback to root WebApplicationContext)
        model - the model attributes for the current view (can be null, using the request attributes for Errors retrieval)
        另请参阅:
        WebApplicationContext, DispatcherServlet
      • RequestContext

        protected RequestContext()
        Default constructor for subclasses.
    • 方法详细资料

      • getRequest

        protected final HttpServletRequest getRequest()
        Return the underlying HttpServletRequest. Only intended for cooperating classes in this package.
      • getServletContext

        protected final ServletContext getServletContext()
        Return the underlying ServletContext. Only intended for cooperating classes in this package.
      • getModel

        public final Map<String,​ObjectgetModel()
        Return the model Map that this RequestContext encapsulates, if any.
        返回:
        the populated model Map, or null if none available
      • getFallbackLocale

        protected Locale getFallbackLocale()
        Determine the fallback locale for this context.

        The default implementation checks for a JSTL locale attribute in request, session or application scope; if not found, returns the HttpServletRequest.getLocale().

        返回:
        the fallback locale (never null)
        另请参阅:
        ServletRequest.getLocale()
      • getFallbackTimeZone

        protected TimeZone getFallbackTimeZone()
        Determine the fallback time zone for this context.

        The default implementation checks for a JSTL time zone attribute in request, session or application scope; returns null if not found.

        返回:
        the fallback time zone (or null if none derivable from the request)
      • getTheme

        public Theme getTheme()
        Return the current theme (never null).

        Resolved lazily for more efficiency when theme support is not being used.

      • getFallbackTheme

        protected Theme getFallbackTheme()
        Determine the fallback theme for this context.

        The default implementation returns the default theme (with name "theme").

        返回:
        the fallback theme (never null)
      • isDefaultHtmlEscape

        public boolean isDefaultHtmlEscape()
        Is default HTML escaping active? Falls back to false in case of no explicit default given.
      • getDefaultHtmlEscape

        public Boolean getDefaultHtmlEscape()
        Return the default HTML escape setting, differentiating between no default specified and an explicit value.
        返回:
        whether default HTML escaping is enabled (null = no explicit default)
      • isResponseEncodedHtmlEscape

        public boolean isResponseEncodedHtmlEscape()
        Is HTML escaping using the response encoding by default? If enabled, only XML markup significant characters will be escaped with UTF-* encodings.

        Falls back to true in case of no explicit default given, as of Spring 4.2.

        从以下版本开始:
        4.1.2
      • getResponseEncodedHtmlEscape

        public Boolean getResponseEncodedHtmlEscape()
        Return the default setting about use of response encoding for HTML escape setting, differentiating between no default specified and an explicit value.
        返回:
        whether default use of response encoding HTML escaping is enabled (null = no explicit default)
        从以下版本开始:
        4.1.2
      • 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.

      • getRequestDataValueProcessor

        public RequestDataValueProcessor getRequestDataValueProcessor()
        Return the RequestDataValueProcessor instance to use obtained from the WebApplicationContext under the name "requestDataValueProcessor". Or null if no matching bean was found.
      • getContextUrl

        public String getContextUrl​(String relativeUrl)
        Return a context-aware URl for the given relative URL.
        参数:
        relativeUrl - the relative URL part
        返回:
        a URL that points back to the server with an absolute path (also URL-encoded accordingly)
      • getContextUrl

        public String getContextUrl​(String relativeUrl,
                                    Map<String,​?> params)
        Return a context-aware URl for the given relative URL with placeholders (named keys with braces {}). For example, send in a relative URL foo/{bar}?spam={spam} and a parameter map {bar=baz,spam=nuts} and the result will be [contextpath]/foo/baz?spam=nuts.
        参数:
        relativeUrl - the relative URL part
        params - a map of parameters to insert as placeholders in the url
        返回:
        a URL that points back to the server with an absolute path (also URL-encoded accordingly)
      • getPathToServlet

        public String getPathToServlet()
        Return the path to URL mappings within the current servlet including the context path and the servlet path of the original request. This is useful for building links to other resources within the application where a servlet mapping of the style "/main/*" is used.

        Delegates to the UrlPathHelper to determine the context and servlet path.

      • getMessage

        public String getMessage​(String code,
                                 String defaultMessage)
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        参数:
        code - code of the message
        defaultMessage - String to return if the lookup fails
        返回:
        the message
      • getMessage

        public String getMessage​(String code,
                                 Object[] args,
                                 String defaultMessage)
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        参数:
        code - code of the message
        args - arguments for the message, or null if none
        defaultMessage - String to return if the lookup fails
        返回:
        the message
      • getMessage

        public String getMessage​(String code,
                                 List<?> args,
                                 String defaultMessage)
        Retrieve the message for the given code, using the "defaultHtmlEscape" setting.
        参数:
        code - code of the message
        args - arguments for the message as a List, or null if none
        defaultMessage - String to return if the lookup fails
        返回:
        the message
      • getMessage

        public String getMessage​(String code,
                                 Object[] args,
                                 String defaultMessage,
                                 boolean htmlEscape)
        Retrieve the message for the given code.
        参数:
        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?
        返回:
        the message
      • getThemeMessage

        public String getThemeMessage​(String code,
                                      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.

        参数:
        code - code of the message
        defaultMessage - String to return if the lookup fails
        返回:
        the message
      • getThemeMessage

        public String getThemeMessage​(String code,
                                      Object[] args,
                                      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.

        参数:
        code - code of the message
        args - arguments for the message, or null if none
        defaultMessage - String to return if the lookup fails
        返回:
        the message
      • getThemeMessage

        public String getThemeMessage​(String code,
                                      List<?> args,
                                      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.

        参数:
        code - code of the message
        args - arguments for the message as a List, or null if none
        defaultMessage - String to return if the lookup fails
        返回:
        the message
      • getThemeMessage

        public String getThemeMessage​(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.

        参数:
        code - code of the message
        返回:
        the message
        抛出:
        NoSuchMessageException - if not found
      • getThemeMessage

        public String getThemeMessage​(String code,
                                      Object[] 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.

        参数:
        code - code of the message
        args - arguments for the message, or null if none
        返回:
        the message
        抛出:
        NoSuchMessageException - if not found
      • getThemeMessage

        public String getThemeMessage​(String code,
                                      List<?> 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.

        参数:
        code - code of the message
        args - arguments for the message as a List, or null if none
        返回:
        the message
        抛出:
        NoSuchMessageException - if not found
      • getErrors

        public Errors getErrors​(String name)
        Retrieve the Errors instance for the given bind object, using the "defaultHtmlEscape" setting.
        参数:
        name - name of the bind object
        返回:
        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.
        参数:
        name - name of the bind object
        htmlEscape - create an Errors instance with automatic HTML escaping?
        返回:
        the Errors instance, or null if not found
      • getModelObject

        protected Object getModelObject​(String modelName)
        Retrieve the model object for the given model name, either from the model or from the request attributes.
        参数:
        modelName - the name of the model object
        返回:
        the model object
      • getBindStatus

        public BindStatus getBindStatus​(String path)
                                 throws IllegalStateException
        Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.
        参数:
        path - the bean and property path for which values and errors will be resolved (e.g. "person.age")
        返回:
        the new BindStatus instance
        抛出:
        IllegalStateException - if no corresponding Errors object found
      • getBindStatus

        public BindStatus getBindStatus​(String path,
                                        boolean htmlEscape)
                                 throws IllegalStateException
        Create a BindStatus for the given bind object, using the "defaultHtmlEscape" setting.
        参数:
        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?
        返回:
        the new BindStatus instance
        抛出:
        IllegalStateException - if no corresponding Errors object found