Class RequestUtils
General purpose utility methods related to processing a servlet request in the Struts controller framework.
- Version:
- $Rev$ $Date$
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic URL
absoluteURL
(HttpServletRequest request, String path) Create and return an absolute URL for the specified context-relative path, based on the server and context information in the specified request.static String
actionIdURL
(String originalPath, ModuleConfig moduleConfig, ActionServlet servlet) Returns the true path of the destination action if the specified forward is an action-aliased URL.static String
actionIdURL
(ForwardConfig forward, HttpServletRequest request, ActionServlet servlet) Returns the true path of the destination action if the specified forward is an action-aliased URL.static String
actionURL
(HttpServletRequest request, ActionConfig action, String pattern) Return the context-relative URL that corresponds to the specifiedActionConfig
, relative to the module associated with the current modules'sModuleConfig
.static Class
<?> applicationClass
(String className) Return theClass
object for the specified fully qualified class name, from this web application's class loader.static Class
<?> applicationClass
(String className, ClassLoader classLoader) Return theClass
object for the specified fully qualified class name, from this web application's class loader.static Object
applicationInstance
(String className) Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader.static Object
applicationInstance
(String className, ClassLoader classLoader) Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader.static ActionForm
createActionForm
(HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) Create (if necessary) and return anActionForm
instance appropriate for this request.static ActionForm
createActionForm
(FormBeanConfig config, ActionServlet servlet) Create and return anActionForm
instance appropriate to the information inconfig
.static StringBuilder
createServerStringBuilder
(String scheme, String server, int port) ReturnStringBuilder
representing the scheme, server, and port number of the current request.static StringBuilder
createServerUriStringBuilder
(String scheme, String server, int port, String uri) ReturnStringBuilder
representing the scheme, server, and port number of the current request.static String
forwardURL
(HttpServletRequest request, ForwardConfig forward) Return the context-relative URL that corresponds to the specifiedForwardConfig
.static String
forwardURL
(HttpServletRequest request, ForwardConfig forward, ModuleConfig moduleConfig) Return the context-relative URL that corresponds to the specifiedForwardConfig
.static String
getServletMapping
(ActionServlet servlet) Retrieves the servlet mapping pattern for the specifiedActionServlet
.static Locale
getUserLocale
(HttpServletRequest request, String locale) Look up and return current user locale, based on the specified parameters.static boolean
isRequestChained
(HttpServletRequest request) Verifies whether current request is forwarded from one action to another or not.static boolean
isRequestForwarded
(HttpServletRequest request) Determines whether the current request is forwarded.static boolean
isRequestIncluded
(HttpServletRequest request) Determines whether the current request is included.static void
populate
(Object bean, HttpServletRequest request) Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name against the corresponding JavaBeans "property setter" methods in the bean's class.static void
populate
(Object bean, String prefix, String suffix, HttpServletRequest request) Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name (plus an optional prefix and/or suffix) against the corresponding JavaBeans "property setter" methods in the bean's class.static void
populate
(ActionRedirect redirect, HttpServletRequest request) Populates the parameters of the specified ActionRedirect from the specified HTTP request.static String
printableURL
(URL url) Compute the printable representation of a URL, leaving off the scheme/host/port part if no host is specified.static StringBuilder
ReturnStringBuilder
representing the scheme, server, and port number of the current request.static StringBuilder
Return the string representing the scheme, server, and port number of the current request.static URL
requestURL
(HttpServletRequest request) Return the URL representing the current request.static URL
serverURL
(HttpServletRequest request) Return the URL representing the scheme, server, and port number of the current request.
-
Field Details
-
CLASS_ACCESS_PATTERN
Pattern matching 'class' access.
-
-
Constructor Details
-
RequestUtils
public RequestUtils()
-
-
Method Details
-
absoluteURL
Create and return an absolute URL for the specified context-relative path, based on the server and context information in the specified request.
- Parameters:
request
- The servlet request we are processingpath
- The context-relative path (must start with '/')- Returns:
- absolute URL based on context-relative path
- Throws:
MalformedURLException
- if we cannot create an absolute URL
-
applicationClass
Return the
Class
object for the specified fully qualified class name, from this web application's class loader.- Parameters:
className
- Fully qualified class name to be loaded- Returns:
- Class object
- Throws:
ClassNotFoundException
- if the class cannot be found
-
applicationClass
public static Class<?> applicationClass(String className, ClassLoader classLoader) throws ClassNotFoundException Return the
Class
object for the specified fully qualified class name, from this web application's class loader.- Parameters:
className
- Fully qualified class name to be loadedclassLoader
- The desired classloader to use- Returns:
- Class object
- Throws:
ClassNotFoundException
- if the class cannot be found
-
applicationInstance
public static Object applicationInstance(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader. The specified class MUST have a public zero-arguments constructor.
- Parameters:
className
- Fully qualified class name to use- Returns:
- new instance of class
- Throws:
ClassNotFoundException
- if the class cannot be foundIllegalAccessException
- if the class or its constructor is not accessibleInstantiationException
- if this class represents an abstract class, an interface, an array class, a primitive type, or voidInstantiationException
- if this class has no zero-arguments constructor
-
applicationInstance
public static Object applicationInstance(String className, ClassLoader classLoader) throws ClassNotFoundException, IllegalAccessException, InstantiationException Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader. The specified class MUST have a public zero-arguments constructor.
- Parameters:
className
- Fully qualified class name to useclassLoader
- The desired classloader to use- Returns:
- new instance of class
- Throws:
ClassNotFoundException
- if the class cannot be foundIllegalAccessException
- if the class or its constructor is not accessibleInstantiationException
- if this class represents an abstract class, an interface, an array class, a primitive type, or voidInstantiationException
- if this class has no zero-arguments constructor
-
createActionForm
public static ActionForm createActionForm(HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet) Create (if necessary) and return an
ActionForm
instance appropriate for this request. If noActionForm
instance is required, returnnull
.- Parameters:
request
- The servlet request we are processingmapping
- The action mapping for this requestmoduleConfig
- The configuration for this moduleservlet
- The action servlet- Returns:
- ActionForm instance associated with this request
-
createActionForm
Create and return an
ActionForm
instance appropriate to the information inconfig
.Does not perform any checks to see if an existing ActionForm exists which could be reused.
- Parameters:
config
- The configuration for the Form bean which is to be created.servlet
- The action servlet- Returns:
- ActionForm instance associated with this request
-
getServletMapping
Retrieves the servlet mapping pattern for the specified
ActionServlet
.- Returns:
- the servlet mapping
- Since:
- Struts 1.3.6
- See Also:
-
getUserLocale
Look up and return current user locale, based on the specified parameters.
- Parameters:
request
- The request used to lookup the Localelocale
- Name of the session attribute for our user's Locale. If this isnull
, the default locale key is used for the lookup.- Returns:
- current user locale
- Since:
- Struts 1.2
-
populate
Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name against the corresponding JavaBeans "property setter" methods in the bean's class. Suitable conversion is done for argument types as described under
convert()
.- Parameters:
bean
- The JavaBean whose properties are to be setrequest
- The HTTP request whose parameters are to be used to populate bean properties- Throws:
ServletException
- if an exception is thrown while setting property values
-
populate
public static void populate(Object bean, String prefix, String suffix, HttpServletRequest request) throws ServletException Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name (plus an optional prefix and/or suffix) against the corresponding JavaBeans "property setter" methods in the bean's class. Suitable conversion is done for argument types as described under
setProperties
.If you specify a non-null
prefix
and a non-nullsuffix
, the parameter name must match both conditions for its value(s) to be used in populating bean properties. If the request's content type is "multipart/form-data" and the method is "POST", theHttpServletRequest
object will be wrapped in aMultipartRequestWrapper
object.- Parameters:
bean
- The JavaBean whose properties are to be setprefix
- The prefix (if any) to be prepend to bean property names when looking for matching parameterssuffix
- The suffix (if any) to be appended to bean property names when looking for matching parametersrequest
- The HTTP request whose parameters are to be used to populate bean properties- Throws:
ServletException
- if an exception is thrown while setting property values
-
populate
Populates the parameters of the specified ActionRedirect from the specified HTTP request.
- Parameters:
redirect
- The ActionRedirect whose parameters are to be setrequest
- The HTTP request whose parameters are to be used- Since:
- Struts 1.4
-
printableURL
Compute the printable representation of a URL, leaving off the scheme/host/port part if no host is specified. This will typically be the case for URLs that were originally created from relative or context-relative URIs.
- Parameters:
url
- URL to render in a printable representation- Returns:
- printable representation of a URL
-
actionURL
Return the context-relative URL that corresponds to the specified
ActionConfig
, relative to the module associated with the current modules'sModuleConfig
.- Parameters:
request
- The servlet request we are processingaction
- ActionConfig to be evaluatedpattern
- URL pattern used to map the controller servlet- Returns:
- context-relative URL relative to the module
- Since:
- Struts 1.1
-
forwardURL
Return the context-relative URL that corresponds to the specified
ForwardConfig
. The URL is calculated based on the properties of theForwardConfig
instance as follows:- If the
contextRelative
property is set, it is assumed that thepath
property contains a path that is already context-relative:- If the
path
property value starts with a slash, it is returned unmodified. - If the
path
property value does not start with a slash, a slash is prepended.
- If the
- Acquire the
forwardPattern
property from theControllerConfig
for the application module used to process this request. If no pattern was configured, default to a pattern of$M$P
, which is compatible with the hard-coded mapping behavior in Struts 1.0. - Process the acquired
forwardPattern
, performing the following substitutions:- $M - Replaced by the module prefix for the application module processing this request.
- $P - Replaced by the
path
property of the specifiedForwardConfig
, prepended with a slash if it does not start with one. - $$ - Replaced by a single dollar sign character.
- $x (where "x" is any charater not listed above) - Silently omit these two characters from the result value. (This has the side effect of causing all other $+letter combinations to be reserved.)
- Parameters:
request
- The servlet request we are processingforward
- ForwardConfig to be evaluated- Returns:
- context-relative URL
- Since:
- Struts 1.1
- If the
-
forwardURL
public static String forwardURL(HttpServletRequest request, ForwardConfig forward, ModuleConfig moduleConfig) Return the context-relative URL that corresponds to the specified
ForwardConfig
. The URL is calculated based on the properties of theForwardConfig
instance as follows:- If the
contextRelative
property is set, it is assumed that thepath
property contains a path that is already context-relative:- If the
path
property value starts with a slash, it is returned unmodified. - If the
path
property value does not start with a slash, a slash is prepended.
- If the
- Acquire the
forwardPattern
property from theControllerConfig
for the application module used to process this request. If no pattern was configured, default to a pattern of$M$P
, which is compatible with the hard-coded mapping behavior in Struts 1.0. - Process the acquired
forwardPattern
, performing the following substitutions:- $M - Replaced by the module prefix for the application module processing this request.
- $P - Replaced by the
path
property of the specifiedForwardConfig
, prepended with a slash if it does not start with one. - $$ - Replaced by a single dollar sign character.
- $x (where "x" is any charater not listed above) - Silently omit these two characters from the result value. (This has the side effect of causing all other $+letter combinations to be reserved.)
- Parameters:
request
- The servlet request we are processingforward
- ForwardConfig to be evaluatedmoduleConfig
- Base forward on this module config.- Returns:
- context-relative URL
- Since:
- Struts 1.2
- If the
-
requestURL
Return the URL representing the current request. This is equivalent to
HttpServletRequest.getRequestURL
in Servlet 2.3.- Parameters:
request
- The servlet request we are processing- Returns:
- URL representing the current request
- Throws:
MalformedURLException
- if a URL cannot be created
-
serverURL
Return the URL representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this.
- Parameters:
request
- The servlet request we are processing- Returns:
- URL representing the scheme, server, and port number of the current request
- Throws:
MalformedURLException
- if a URL cannot be created
-
requestToServerUriStringBuilder
Return the string representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this.
- Parameters:
request
- The servlet request we are processing- Returns:
- URL representing the scheme, server, and port number of the current request
- Since:
- Struts 1.2.0
-
requestToServerStringBuilder
Return
StringBuilder
representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this.- Parameters:
request
- The servlet request we are processing- Returns:
- URL representing the scheme, server, and port number of the current request
- Since:
- Struts 1.2.0
-
createServerStringBuilder
Return
StringBuilder
representing the scheme, server, and port number of the current request.- Parameters:
scheme
- The scheme name to useserver
- The server name to useport
- The port value to use- Returns:
- StringBuilder in the form scheme: server: port
- Since:
- Struts 1.2.0
-
createServerUriStringBuilder
public static StringBuilder createServerUriStringBuilder(String scheme, String server, int port, String uri) Return
StringBuilder
representing the scheme, server, and port number of the current request.- Parameters:
scheme
- The scheme name to useserver
- The server name to useport
- The port value to useuri
- The uri value to use- Returns:
- StringBuilder in the form scheme: server: port
- Since:
- Struts 1.2.0
-
actionIdURL
public static String actionIdURL(ForwardConfig forward, HttpServletRequest request, ActionServlet servlet) Returns the true path of the destination action if the specified forward is an action-aliased URL. This method version forms the URL based on the current request; selecting the current module if the forward does not explicitly contain a module path.
- Parameters:
forward
- the forward configrequest
- the current requestservlet
- the servlet handling the current request- Returns:
- the context-relative URL of the action if the forward has an action identifier; otherwise
null
. - Since:
- Struts 1.3.6
-
actionIdURL
public static String actionIdURL(String originalPath, ModuleConfig moduleConfig, ActionServlet servlet) Returns the true path of the destination action if the specified forward is an action-aliased URL. This method version forms the URL based on the specified module.
- Parameters:
originalPath
- the action-aliased pathmoduleConfig
- the module config for this requestservlet
- the servlet handling the current request- Returns:
- the context-relative URL of the action if the path has an action identifier; otherwise
null
. - Since:
- Struts 1.3.6
-
isRequestForwarded
Determines whether the current request is forwarded.- Parameters:
request
- current HTTP request- Returns:
- true if the request is forwarded; otherwise false
- Since:
- Struts 1.4
-
isRequestIncluded
Determines whether the current request is included.- Parameters:
request
- current HTTP request- Returns:
- true if the request is included; otherwise false
- Since:
- Struts 1.4
-
isRequestChained
Verifies whether current request is forwarded from one action to another or not.- Parameters:
request
- current HTTP request- Returns:
- true if the request is chained; otherwise false
- Since:
- Struts 1.4
-