类 RequestContextUtils
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
-
嵌套类概要
嵌套类修饰符和类型类说明private static classprivate static classprivate static classprivate static classFactory that exposes the current request-context object on demand.private static classprivate static classprivate static classprivate static class(专用程序包) static final classFactory that exposes the current web-session object on demand. -
字段概要
字段修饰符和类型字段说明private static final RequestContextUtils.BooleanParserprivate static final RequestContextUtils.DoubleParserprivate static final RequestContextUtils.FloatParserprivate static final RequestContextUtils.IntParserprivate static final RequestContextUtils.LongParserprivate static final RequestContextUtils.StringParser -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private static voidaddParam(String s, int nameStart, int valueStart, int valueEnd, cn.taketoday.util.MultiValueMap<String, String> params) static <T> TgetBean(RequestContext request, Class<T> requiredType) static <T> TgetBean(RequestContext request, String beanName) static <T> TgetBean(RequestContext request, String beanName, Class<T> requiredType) static BooleangetBooleanParameter(RequestContext request, String name) Get a Boolean parameter, ornullif not present.static booleangetBooleanParameter(RequestContext request, String name, boolean defaultVal) Get a boolean parameter, with a fallback value.static boolean[]getBooleanParameters(RequestContext request, String name) Get an array of boolean parameters, return an empty array if not found.static DoublegetDoubleParameter(RequestContext request, String name) Get a Double parameter, ornullif not present.static doublegetDoubleParameter(RequestContext request, String name, double defaultVal) Get a double parameter, with a fallback value.static double[]getDoubleParameters(RequestContext request, String name) Get an array of double parameters, return an empty array if not found.static FloatgetFloatParameter(RequestContext request, String name) Get a Float parameter, ornullif not present.static floatgetFloatParameter(RequestContext request, String name, float defaultVal) Get a float parameter, with a fallback value.static float[]getFloatParameters(RequestContext request, String name) Get an array of float parameters, return an empty array if not found.static IntegergetIntParameter(RequestContext request, String name) Get an Integer parameter, ornullif not present.static intgetIntParameter(RequestContext request, String name, int defaultVal) Get an int parameter, with a fallback value.static int[]getIntParameters(RequestContext request, String name) Get an array of int parameters, return an empty array if not found.static LocalegetLocale(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.static LocaleResolvergetLocaleResolver(RequestContext request) Return the LocaleResolver that has been bound to the request by the RequestContext.static LonggetLongParameter(RequestContext request, String name) Get a Long parameter, ornullif not present.static longgetLongParameter(RequestContext request, String name, long defaultVal) Get a long parameter, with a fallback value.static long[]getLongParameters(RequestContext request, String name) Get an array of long parameters, return an empty array if not found.static RedirectModelgetOutputRedirectModel(RequestContext request) Return "output" RedirectModel to save attributes for request after redirect.static RedirectModelManagergetRedirectModelManager(RequestContext request) Return theRedirectModelManagerinstance to save flash attributes.static booleangetRequiredBooleanParameter(RequestContext request, String name) Get a boolean parameter, throwing an exception if it isn't found or isn't a boolean.static boolean[]getRequiredBooleanParameters(RequestContext request, String name) Get an array of boolean parameters, throwing an exception if not found or one isn't a boolean.static doublegetRequiredDoubleParameter(RequestContext request, String name) Get a double parameter, throwing an exception if it isn't found or isn't a number.static double[]getRequiredDoubleParameters(RequestContext request, String name) Get an array of double parameters, throwing an exception if not found or one is not a number.static floatgetRequiredFloatParameter(RequestContext request, String name) Get a float parameter, throwing an exception if it isn't found or isn't a number.static float[]getRequiredFloatParameters(RequestContext request, String name) Get an array of float parameters, throwing an exception if not found or one is not a number.static intgetRequiredIntParameter(RequestContext request, String name) Get an int parameter, throwing an exception if it isn't found or isn't a number.static int[]getRequiredIntParameters(RequestContext request, String name) Get an array of int parameters, throwing an exception if not found or one is not a number..static longgetRequiredLongParameter(RequestContext request, String name) Get a long parameter, throwing an exception if it isn't found or isn't a number.static long[]getRequiredLongParameters(RequestContext request, String name) Get an array of long parameters, throwing an exception if not found or one is not a number.static WebSessiongetRequiredSession(RequestContext request) Returns the current session associated with this request, or if the request does not have a session, creates one.static StringgetRequiredStringParameter(RequestContext request, String name) Get a String parameter, throwing an exception if it isn't found.static String[]getRequiredStringParameters(RequestContext request, String name) Get an array of String parameters, throwing an exception if not found.static WebSessiongetSession(RequestContext request) Returns the current session associated with this request, or if the request does not have a session, creates one.static WebSessiongetSession(RequestContext request, boolean create) Returns the currentWebSessionassociated with this request or, if there is no current session andcreateis true, returns a new session.static StringgetSessionId(RequestContext request) Determine the session id of the given request, if any.static SessionManagergetSessionManager(RequestContext request) Return the WebSessionManagerstatic StringgetStringParameter(RequestContext request, String name) Get a String parameter, ornullif not present.static StringgetStringParameter(RequestContext request, String name, String defaultVal) Get a String parameter, with a fallback value.static String[]getStringParameters(RequestContext request, String name) Get an array of String parameters, return an empty array if not found.static TimeZonegetTimeZone(RequestContext request) Retrieve the current time zone from the given request, using theTimeZoneAwareLocaleContextin theLocaleResolverbound to the request by theDispatcherServlet(if available).static voidparseParameters(cn.taketoday.util.MultiValueMap<String, String> parameterMap, String s) Parse Parametersstatic voidregisterScopes(cn.taketoday.beans.factory.config.ConfigurableBeanFactory beanFactory) Register web-specific scopes ("request", "session") with the given BeanFactory, as used by the ApplicationContext.static voidsaveRedirectModel(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 theRedirectModelManager.static voidsaveRedirectModel(String location, RequestContext request, 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 theRedirectModelManager.
-
字段详细资料
-
INT_PARSER
-
LONG_PARSER
-
FLOAT_PARSER
-
DOUBLE_PARSER
-
BOOLEAN_PARSER
-
STRING_PARSER
-
-
构造器详细资料
-
RequestContextUtils
public RequestContextUtils()
-
-
方法详细资料
-
getBean
-
getBean
-
getBean
@Nullable public static <T> T getBean(RequestContext request, String beanName, Class<T> requiredType) -
getSessionId
Determine the session id of the given request, if any.- 参数:
request- current HTTP request- 返回:
- the session id, or
nullif none
-
getSession
Returns the current session associated with this request, or if the request does not have a session, creates one.- 参数:
request- Current request- 返回:
- the
WebSessionassociated with this request - 另请参阅:
-
getRequiredSession
Returns the current session associated with this request, or if the request does not have a session, creates one.- 参数:
request- Current request- 返回:
- the
WebSessionassociated with this request - 另请参阅:
-
getSession
Returns the currentWebSessionassociated with this request or, if there is no current session andcreateis true, returns a new session.If
createisfalseand the request has no validWebSession, this method returnsnull.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 requestcreate-trueto create a new session for this request if necessary;falseto returnnullif there's no current session- 返回:
- the
WebSessionassociated with this request ornullifcreateisfalseand the request has no valid session - 另请参阅:
-
getSessionManager
Return the WebSessionManager- 参数:
request- current HTTP request- 返回:
- the current LocaleResolver, or
nullif not found
-
getLocaleResolver
Return the LocaleResolver that has been bound to the request by the RequestContext.- 参数:
request- current HTTP request- 返回:
- the current LocaleResolver, or
nullif not found
-
getLocale
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
- 另请参阅:
-
getLocaleResolver(cn.taketoday.web.RequestContext)LocaleContextHolder.getLocale()
-
getTimeZone
Retrieve the current time zone from the given request, using theTimeZoneAwareLocaleContextin theLocaleResolverbound to the request by theDispatcherServlet(if available).Note: This method returns
nullif no specific time zone can be resolved for the given request. This is in contrast togetLocale(cn.taketoday.web.RequestContext)where it is always possible to fall back to the request's locale based on theAccept-Languageheader or the default locale for the server.Consider using
LocaleContextHolder.getTimeZone()which will normally be populated with the sameTimeZone: that method only differs in terms of its fallback to the system time zone if theLocaleResolverhasn't provided a specific time zone (instead of this method'snull).- 参数:
request- current HTTP request- 返回:
- the current time zone for the given request, either from the
TimeZoneAwareLocaleContextornullif none associated - 另请参阅:
-
getLocaleResolver(cn.taketoday.web.RequestContext)LocaleContextHolder.getTimeZone()
-
getOutputRedirectModel
Return "output" RedirectModel to save attributes for request after redirect.- 参数:
request- current request- 返回:
- a
RedirectModelinstance
-
getRedirectModelManager
Return theRedirectModelManagerinstance 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
RedirectModelManagerinstance
-
saveRedirectModel
Convenience method that retrieves the"output" RedirectModel, updates it with the path and query params of the target URL, and then saves it using theRedirectModelManager.- 参数:
location- the target URL for the redirectrequest- 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 theRedirectModelManager.- 参数:
location- the target URL for the redirectrequest- the current requestmanager- RedirectModelManager
-
parseParameters
-
parseParameters
Parse Parameters- 参数:
s- decodedString- 返回:
- Map of list parameters
-
addParam
-
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, ornullif not present. Throws an exception if it the parameter value isn't a number.- 参数:
request- current HTTP requestname- the name of the parameter- 返回:
- the Integer value, or
nullif not present - 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-
getIntParameter
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 requestname- the name of the parameterdefaultVal- the default value to use as fallback
-
getIntParameters
Get an array of int parameters, return an empty array if not found.- 参数:
request- current HTTP requestname- 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 requestname- 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 requestname- 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, ornullif not present. Throws an exception if it the parameter value isn't a number.- 参数:
request- current HTTP requestname- the name of the parameter- 返回:
- the Long value, or
nullif not present - 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-
getLongParameter
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 requestname- the name of the parameterdefaultVal- the default value to use as fallback
-
getLongParameters
Get an array of long parameters, return an empty array if not found.- 参数:
request- current HTTP requestname- 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 requestname- 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 requestname- 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, ornullif not present. Throws an exception if it the parameter value isn't a number.- 参数:
request- current HTTP requestname- the name of the parameter- 返回:
- the Float value, or
nullif not present - 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-
getFloatParameter
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 requestname- the name of the parameterdefaultVal- the default value to use as fallback
-
getFloatParameters
Get an array of float parameters, return an empty array if not found.- 参数:
request- current HTTP requestname- 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 requestname- 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 requestname- 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, ornullif not present. Throws an exception if it the parameter value isn't a number.- 参数:
request- current HTTP requestname- the name of the parameter- 返回:
- the Double value, or
nullif not present - 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-
getDoubleParameter
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 requestname- the name of the parameterdefaultVal- the default value to use as fallback
-
getDoubleParameters
Get an array of double parameters, return an empty array if not found.- 参数:
request- current HTTP requestname- 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 requestname- 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 requestname- 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, ornullif 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 requestname- the name of the parameter- 返回:
- the Boolean value, or
nullif not present - 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-
getBooleanParameter
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 requestname- the name of the parameterdefaultVal- the default value to use as fallback
-
getBooleanParameters
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 requestname- 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 requestname- 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 requestname- 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, ornullif not present.- 参数:
request- current HTTP requestname- the name of the parameter- 返回:
- the String value, or
nullif not present - 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-
getStringParameter
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 requestname- the name of the parameterdefaultVal- the default value to use as fallback
-
getStringParameters
Get an array of String parameters, return an empty array if not found.- 参数:
request- current HTTP requestname- 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 requestname- 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 requestname- the name of the parameter- 抛出:
RequestBindingException- a subclass of ServletException, so it doesn't need to be caught
-