程序包 cn.taketoday.web

类 RequestContextUtils

java.lang.Object
cn.taketoday.web.RequestContextUtils

public class RequestContextUtils extends Object
Parameter extraction methods, for an approach distinct from data binding, in which parameters of specific types are required.

This approach is very useful for simple submissions, where binding request parameters to a command object would be overkill.

从以下版本开始:
4.0 2022/1/22 23:21
作者:
Harry Yang
  • 字段详细资料

  • 构造器详细资料

    • RequestContextUtils

      public RequestContextUtils()
  • 方法详细资料

    • getBean

      @Nullable public static <T> T getBean(RequestContext request, Class<T> requiredType)
    • getBean

      @Nullable public static <T> T getBean(RequestContext request, String beanName)
    • getBean

      @Nullable public static <T> T getBean(RequestContext request, String beanName, Class<T> requiredType)
    • getSessionId

      @Nullable public static String getSessionId(RequestContext request)
      Determine the session id of the given request, if any.
      参数:
      request - current HTTP request
      返回:
      the session id, or null if none
    • getSession

      @Nullable public static WebSession getSession(RequestContext request)
      Returns the current session associated with this request, or if the request does not have a session, creates one.
      参数:
      request - Current request
      返回:
      the WebSession associated with this request
      另请参阅:
    • getRequiredSession

      public static WebSession getRequiredSession(RequestContext request)
      Returns the current session associated with this request, or if the request does not have a session, creates one.
      参数:
      request - Current request
      返回:
      the WebSession associated with this request
      另请参阅:
    • getSession

      @Nullable public static WebSession getSession(RequestContext request, boolean create)
      Returns the current WebSession associated with this request or, if there is no current session and create is true, returns a new session.

      If create is false and the request has no valid WebSession, this method returns null.

      To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.

      参数:
      request - Current request
      create - true to create a new session for this request if necessary; false to return null if there's no current session
      返回:
      the WebSession associated with this request or null if create is false and the request has no valid session
      另请参阅:
    • getSessionManager

      @Nullable public static SessionManager getSessionManager(RequestContext request)
      Return the WebSessionManager
      参数:
      request - current HTTP request
      返回:
      the current LocaleResolver, or null if not found
    • getLocaleResolver

      @Nullable public static LocaleResolver getLocaleResolver(RequestContext request)
      Return the LocaleResolver that has been bound to the request by the RequestContext.
      参数:
      request - current HTTP request
      返回:
      the current LocaleResolver, or null if not found
    • getLocale

      public static Locale getLocale(RequestContext request)
      Retrieve the current locale from the given request, using the LocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the request's accept-header Locale.

      This method serves as a straightforward alternative to the standard Servlet ServletRequest.getLocale() method, falling back to the latter if no more specific locale has been found.

      Consider using LocaleContextHolder.getLocale() which will normally be populated with the same Locale.

      参数:
      request - current HTTP request
      返回:
      the current locale for the given request, either from the LocaleResolver or from the plain request itself
      另请参阅:
    • getTimeZone

      @Nullable public static TimeZone getTimeZone(RequestContext request)
      Retrieve the current time zone from the given request, using the TimeZoneAwareLocaleContext in the LocaleResolver bound to the request by the DispatcherServlet (if available).

      Note: This method returns null if no specific time zone can be resolved for the given request. This is in contrast to getLocale(cn.taketoday.web.RequestContext) where it is always possible to fall back to the request's locale based on the Accept-Language header or the default locale for the server.

      Consider using LocaleContextHolder.getTimeZone() which will normally be populated with the same TimeZone: that method only differs in terms of its fallback to the system time zone if the LocaleResolver hasn't provided a specific time zone (instead of this method's null).

      参数:
      request - current HTTP request
      返回:
      the current time zone for the given request, either from the TimeZoneAwareLocaleContext or null if none associated
      另请参阅:
    • getOutputRedirectModel

      @Nullable public static RedirectModel getOutputRedirectModel(RequestContext request)
      Return "output" RedirectModel to save attributes for request after redirect.
      参数:
      request - current request
      返回:
      a RedirectModel instance
    • getRedirectModelManager

      @Nullable public static RedirectModelManager getRedirectModelManager(RequestContext request)
      Return the RedirectModelManager instance to save flash attributes.

      the convenience method saveRedirectModel(java.lang.String, cn.taketoday.web.RequestContext) may be used to save the "output" RedirectModel.

      参数:
      request - the current request
      返回:
      a RedirectModelManager instance
    • saveRedirectModel

      public static void saveRedirectModel(String location, RequestContext request)
      Convenience method that retrieves the "output" RedirectModel, updates it with the path and query params of the target URL, and then saves it using the RedirectModelManager.
      参数:
      location - the target URL for the redirect
      request - the current request
    • saveRedirectModel

      public static void saveRedirectModel(String location, RequestContext request, @Nullable RedirectModelManager manager)
      Convenience method that retrieves the "output" RedirectModel, updates it with the path and query params of the target URL, and then saves it using the RedirectModelManager.
      参数:
      location - the target URL for the redirect
      request - the current request
      manager - RedirectModelManager
    • parseParameters

      public static void parseParameters(cn.taketoday.util.MultiValueMap<String,String> parameterMap, String s)
    • parseParameters

      public static cn.taketoday.util.MultiValueMap<String,String> parseParameters(String s)
      Parse Parameters
      参数:
      s - decoded String
      返回:
      Map of list parameters
    • addParam

      private static void addParam(String s, int nameStart, int valueStart, int valueEnd, cn.taketoday.util.MultiValueMap<String,String> params)
    • registerScopes

      public static void registerScopes(cn.taketoday.beans.factory.config.ConfigurableBeanFactory beanFactory)
      Register web-specific scopes ("request", "session") with the given BeanFactory, as used by the ApplicationContext.
      参数:
      beanFactory - the BeanFactory to configure
    • getIntParameter

      @Nullable public static Integer getIntParameter(RequestContext request, String name) throws RequestBindingException
      Get an Integer parameter, or null if not present. Throws an exception if it the parameter value isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      返回:
      the Integer value, or null if not present
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getIntParameter

      public static int getIntParameter(RequestContext request, String name, int defaultVal)
      Get an int parameter, with a fallback value. Never throws an exception. Can pass a distinguished value as default to enable checks of whether it was supplied.
      参数:
      request - current HTTP request
      name - the name of the parameter
      defaultVal - the default value to use as fallback
    • getIntParameters

      public static int[] getIntParameters(RequestContext request, String name)
      Get an array of int parameters, return an empty array if not found.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
    • getRequiredIntParameter

      public static int getRequiredIntParameter(RequestContext request, String name) throws RequestBindingException
      Get an int parameter, throwing an exception if it isn't found or isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getRequiredIntParameters

      public static int[] getRequiredIntParameters(RequestContext request, String name) throws RequestBindingException
      Get an array of int parameters, throwing an exception if not found or one is not a number..
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getLongParameter

      @Nullable public static Long getLongParameter(RequestContext request, String name) throws RequestBindingException
      Get a Long parameter, or null if not present. Throws an exception if it the parameter value isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      返回:
      the Long value, or null if not present
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getLongParameter

      public static long getLongParameter(RequestContext request, String name, long defaultVal)
      Get a long parameter, with a fallback value. Never throws an exception. Can pass a distinguished value as default to enable checks of whether it was supplied.
      参数:
      request - current HTTP request
      name - the name of the parameter
      defaultVal - the default value to use as fallback
    • getLongParameters

      public static long[] getLongParameters(RequestContext request, String name)
      Get an array of long parameters, return an empty array if not found.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
    • getRequiredLongParameter

      public static long getRequiredLongParameter(RequestContext request, String name) throws RequestBindingException
      Get a long parameter, throwing an exception if it isn't found or isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getRequiredLongParameters

      public static long[] getRequiredLongParameters(RequestContext request, String name) throws RequestBindingException
      Get an array of long parameters, throwing an exception if not found or one is not a number.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getFloatParameter

      @Nullable public static Float getFloatParameter(RequestContext request, String name) throws RequestBindingException
      Get a Float parameter, or null if not present. Throws an exception if it the parameter value isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      返回:
      the Float value, or null if not present
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getFloatParameter

      public static float getFloatParameter(RequestContext request, String name, float defaultVal)
      Get a float parameter, with a fallback value. Never throws an exception. Can pass a distinguished value as default to enable checks of whether it was supplied.
      参数:
      request - current HTTP request
      name - the name of the parameter
      defaultVal - the default value to use as fallback
    • getFloatParameters

      public static float[] getFloatParameters(RequestContext request, String name)
      Get an array of float parameters, return an empty array if not found.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
    • getRequiredFloatParameter

      public static float getRequiredFloatParameter(RequestContext request, String name) throws RequestBindingException
      Get a float parameter, throwing an exception if it isn't found or isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getRequiredFloatParameters

      public static float[] getRequiredFloatParameters(RequestContext request, String name) throws RequestBindingException
      Get an array of float parameters, throwing an exception if not found or one is not a number.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getDoubleParameter

      @Nullable public static Double getDoubleParameter(RequestContext request, String name) throws RequestBindingException
      Get a Double parameter, or null if not present. Throws an exception if it the parameter value isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      返回:
      the Double value, or null if not present
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getDoubleParameter

      public static double getDoubleParameter(RequestContext request, String name, double defaultVal)
      Get a double parameter, with a fallback value. Never throws an exception. Can pass a distinguished value as default to enable checks of whether it was supplied.
      参数:
      request - current HTTP request
      name - the name of the parameter
      defaultVal - the default value to use as fallback
    • getDoubleParameters

      public static double[] getDoubleParameters(RequestContext request, String name)
      Get an array of double parameters, return an empty array if not found.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
    • getRequiredDoubleParameter

      public static double getRequiredDoubleParameter(RequestContext request, String name) throws RequestBindingException
      Get a double parameter, throwing an exception if it isn't found or isn't a number.
      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getRequiredDoubleParameters

      public static double[] getRequiredDoubleParameters(RequestContext request, String name) throws RequestBindingException
      Get an array of double parameters, throwing an exception if not found or one is not a number.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getBooleanParameter

      @Nullable public static Boolean getBooleanParameter(RequestContext request, String name) throws RequestBindingException
      Get a Boolean parameter, or null if not present. Throws an exception if it the parameter value isn't a boolean.

      Accepts "true", "on", "yes" (any case) and "1" as values for true; treats every other non-empty value as false (i.e. parses leniently).

      参数:
      request - current HTTP request
      name - the name of the parameter
      返回:
      the Boolean value, or null if not present
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getBooleanParameter

      public static boolean getBooleanParameter(RequestContext request, String name, boolean defaultVal)
      Get a boolean parameter, with a fallback value. Never throws an exception. Can pass a distinguished value as default to enable checks of whether it was supplied.

      Accepts "true", "on", "yes" (any case) and "1" as values for true; treats every other non-empty value as false (i.e. parses leniently).

      参数:
      request - current HTTP request
      name - the name of the parameter
      defaultVal - the default value to use as fallback
    • getBooleanParameters

      public static boolean[] getBooleanParameters(RequestContext request, String name)
      Get an array of boolean parameters, return an empty array if not found.

      Accepts "true", "on", "yes" (any case) and "1" as values for true; treats every other non-empty value as false (i.e. parses leniently).

      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
    • getRequiredBooleanParameter

      public static boolean getRequiredBooleanParameter(RequestContext request, String name) throws RequestBindingException
      Get a boolean parameter, throwing an exception if it isn't found or isn't a boolean.

      Accepts "true", "on", "yes" (any case) and "1" as values for true; treats every other non-empty value as false (i.e. parses leniently).

      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getRequiredBooleanParameters

      public static boolean[] getRequiredBooleanParameters(RequestContext request, String name) throws RequestBindingException
      Get an array of boolean parameters, throwing an exception if not found or one isn't a boolean.

      Accepts "true", "on", "yes" (any case) and "1" as values for true; treats every other non-empty value as false (i.e. parses leniently).

      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getStringParameter

      @Nullable public static String getStringParameter(RequestContext request, String name) throws RequestBindingException
      Get a String parameter, or null if not present.
      参数:
      request - current HTTP request
      name - the name of the parameter
      返回:
      the String value, or null if not present
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getStringParameter

      public static String getStringParameter(RequestContext request, String name, String defaultVal)
      Get a String parameter, with a fallback value. Never throws an exception. Can pass a distinguished value to default to enable checks of whether it was supplied.
      参数:
      request - current HTTP request
      name - the name of the parameter
      defaultVal - the default value to use as fallback
    • getStringParameters

      public static String[] getStringParameters(RequestContext request, String name)
      Get an array of String parameters, return an empty array if not found.
      参数:
      request - current HTTP request
      name - the name of the parameter with multiple possible values
    • getRequiredStringParameter

      public static String getRequiredStringParameter(RequestContext request, String name) throws RequestBindingException
      Get a String parameter, throwing an exception if it isn't found.
      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught
    • getRequiredStringParameters

      public static String[] getRequiredStringParameters(RequestContext request, String name) throws RequestBindingException
      Get an array of String parameters, throwing an exception if not found.
      参数:
      request - current HTTP request
      name - the name of the parameter
      抛出:
      RequestBindingException - a subclass of ServletException, so it doesn't need to be caught