类 PortletUtils
- java.lang.Object
- org.springframework.web.portlet.util.PortletUtils
public abstract class PortletUtils extends Object
Miscellaneous utilities for portlet applications. Used by various framework classes.- 从以下版本开始:
- 2.0
- 作者:
- Juergen Hoeller, William G. Thompson, Jr., John A. Lewis
构造器概要
构造器 构造器 说明 PortletUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static void
clearAllRenderParameters(ActionResponse response)
Clear all the render parameters from theActionResponse
.static void
exposeRequestAttributes(PortletRequest request, Map<String,?> attributes)
Expose the given Map as request attributes, using the keys as attribute names and the values as corresponding attribute values.static Cookie
getCookie(PortletRequest request, String name)
Retrieve the first cookie with the given name.static <T> T
getNativeRequest(PortletRequest request, Class<T> requiredType)
Return an appropriate request object of the specified type, if available, unwrapping the given request as far as necessary.static <T> T
getNativeResponse(PortletResponse response, Class<T> requiredType)
Return an appropriate response object of the specified type, if available, unwrapping the given response as far as necessary.static Object
getOrCreateSessionAttribute(PortletSession session, String name, Class<?> clazz)
Get the specified session attribute under thePortletSession.PORTLET_SCOPE
, creating and setting a new attribute if no existing found.static Object
getOrCreateSessionAttribute(PortletSession session, String name, Class<?> clazz, int scope)
Get the specified session attribute in the given scope, creating and setting a new attribute if no existing found.static Map<String,Object>
getParametersStartingWith(PortletRequest request, String prefix)
Return a map containing all parameters with the given prefix.static String
getRealPath(PortletContext portletContext, String path)
Return the real path of the given path within the web application, as provided by the portlet container.static Object
getRequiredSessionAttribute(PortletRequest request, String name)
Check the given request for a session attribute of the given name under thePortletSession.PORTLET_SCOPE
.static Object
getRequiredSessionAttribute(PortletRequest request, String name, int scope)
Check the given request for a session attribute of the given name in the given scope.static Object
getSessionAttribute(PortletRequest request, String name)
Check the given request for a session attribute of the given name under thePortletSession.PORTLET_SCOPE
.static Object
getSessionAttribute(PortletRequest request, String name, int scope)
Check the given request for a session attribute of the given name in the given scope.static Object
getSessionMutex(PortletSession session)
Return the best available mutex for the given session: that is, an object to synchronize on for the given session.static String
getSubmitParameter(PortletRequest request, String name)
Return the full name of a specific input type="submit" parameter if it was sent in the request, either via a button (directly with name) or via an image (name + ".x" or name + ".y").static int
getTargetPage(PortletRequest request, String paramPrefix, int currentPage)
Return the target page specified in the request.static File
getTempDir(PortletContext portletContext)
Return the temporary directory for the current web application, as provided by the portlet container.static boolean
hasSubmitParameter(PortletRequest request, String name)
Check if a specific input type="submit" parameter was sent in the request, either via a button (directly with name) or via an image (name + ".x" or name + ".y").static void
passAllParametersToRenderPhase(ActionRequest request, ActionResponse response)
Pass all the action request parameters to the render phase by putting them into the action response object.static void
serveResource(ResourceRequest request, ResourceResponse response, PortletContext context)
Serve the resource as specified in the given request to the given response, using the PortletContext's request dispatcher.static void
setSessionAttribute(PortletRequest request, String name, Object value)
Set the session attribute with the given name to the given value under thePortletSession.PORTLET_SCOPE
.static void
setSessionAttribute(PortletRequest request, String name, Object value, int scope)
Set the session attribute with the given name to the given value in the given scope.
构造器详细资料
PortletUtils
public PortletUtils()
方法详细资料
getTempDir
public static File getTempDir(PortletContext portletContext)
Return the temporary directory for the current web application, as provided by the portlet container.- 参数:
portletContext
- the portlet context of the web application- 返回:
- the File representing the temporary directory
getRealPath
public static String getRealPath(PortletContext portletContext, String path) throws FileNotFoundException
Return the real path of the given path within the web application, as provided by the portlet container.Prepends a slash if the path does not already start with a slash, and throws a
FileNotFoundException
if the path cannot be resolved to a resource (in contrast toPortletContext's {@code getRealPath}
, which simply returnsnull
).- 参数:
portletContext
- the portlet context of the web applicationpath
- the relative path within the web application- 返回:
- the corresponding real path
- 抛出:
FileNotFoundException
- if the path cannot be resolved to a resource- 另请参阅:
PortletContext.getRealPath(java.lang.String)
getSessionAttribute
public static Object getSessionAttribute(PortletRequest request, String name)
Check the given request for a session attribute of the given name under thePortletSession.PORTLET_SCOPE
. Returnsnull
if there is no session or if the session has no such attribute in that scope. Does not create a new session if none has existed before!- 参数:
request
- current portlet requestname
- the name of the session attribute- 返回:
- the value of the session attribute, or
null
if not found
getSessionAttribute
public static Object getSessionAttribute(PortletRequest request, String name, int scope)
Check the given request for a session attribute of the given name in the given scope. Returnsnull
if there is no session or if the session has no such attribute in that scope. Does not create a new session if none has existed before!- 参数:
request
- current portlet requestname
- the name of the session attributescope
- session scope of this attribute- 返回:
- the value of the session attribute, or
null
if not found
getRequiredSessionAttribute
public static Object getRequiredSessionAttribute(PortletRequest request, String name) throws IllegalStateException
Check the given request for a session attribute of the given name under thePortletSession.PORTLET_SCOPE
. Throws an exception if there is no session or if the session has no such attribute in that scope.Does not create a new session if none has existed before!
- 参数:
request
- current portlet requestname
- the name of the session attribute- 返回:
- the value of the session attribute
- 抛出:
IllegalStateException
- if the session attribute could not be found
getRequiredSessionAttribute
public static Object getRequiredSessionAttribute(PortletRequest request, String name, int scope) throws IllegalStateException
Check the given request for a session attribute of the given name in the given scope. Throws an exception if there is no session or if the session has no such attribute in that scope.Does not create a new session if none has existed before!
- 参数:
request
- current portlet requestname
- the name of the session attributescope
- session scope of this attribute- 返回:
- the value of the session attribute
- 抛出:
IllegalStateException
- if the session attribute could not be found
setSessionAttribute
public static void setSessionAttribute(PortletRequest request, String name, Object value)
Set the session attribute with the given name to the given value under thePortletSession.PORTLET_SCOPE
. Removes the session attribute if value isnull
, if a session existed at all. Does not create a new session if not necessary!- 参数:
request
- current portlet requestname
- the name of the session attributevalue
- the value of the session attribute
setSessionAttribute
public static void setSessionAttribute(PortletRequest request, String name, Object value, int scope)
Set the session attribute with the given name to the given value in the given scope. Removes the session attribute if value isnull
, if a session existed at all. Does not create a new session if not necessary!- 参数:
request
- current portlet requestname
- the name of the session attributevalue
- the value of the session attributescope
- session scope of this attribute
getOrCreateSessionAttribute
public static Object getOrCreateSessionAttribute(PortletSession session, String name, Class<?> clazz) throws IllegalArgumentException
Get the specified session attribute under thePortletSession.PORTLET_SCOPE
, creating and setting a new attribute if no existing found. The given class needs to have a public no-arg constructor. Useful for on-demand state objects in a web tier, like shopping carts.- 参数:
session
- current portlet sessionname
- the name of the session attributeclazz
- the class to instantiate for a new attribute- 返回:
- the value of the session attribute, newly created if not found
- 抛出:
IllegalArgumentException
- if the session attribute could not be instantiated
getOrCreateSessionAttribute
public static Object getOrCreateSessionAttribute(PortletSession session, String name, Class<?> clazz, int scope) throws IllegalArgumentException
Get the specified session attribute in the given scope, creating and setting a new attribute if no existing found. The given class needs to have a public no-arg constructor. Useful for on-demand state objects in a web tier, like shopping carts.- 参数:
session
- current portlet sessionname
- the name of the session attributeclazz
- the class to instantiate for a new attributescope
- the session scope of this attribute- 返回:
- the value of the session attribute, newly created if not found
- 抛出:
IllegalArgumentException
- if the session attribute could not be instantiated
getSessionMutex
public static Object getSessionMutex(PortletSession session)
Return the best available mutex for the given session: that is, an object to synchronize on for the given session.Returns the session mutex attribute if available; usually, this means that the
HttpSessionMutexListener
needs to be defined inweb.xml
. Falls back to thePortletSession
itself if no mutex attribute found.The session mutex is guaranteed to be the same object during the entire lifetime of the session, available under the key defined by the
WebUtils.SESSION_MUTEX_ATTRIBUTE
constant. It serves as a safe reference to synchronize on for locking on the current session.In many cases, the
PortletSession
reference itself is a safe mutex as well, since it will always be the same object reference for the same active logical session. However, this is not guaranteed across different servlet containers; the only 100% safe way is a session mutex.- 参数:
session
- the HttpSession to find a mutex for- 返回:
- the mutex object (never
null
) - 另请参阅:
WebUtils.SESSION_MUTEX_ATTRIBUTE
,HttpSessionMutexListener
getNativeRequest
public static <T> T getNativeRequest(PortletRequest request, Class<T> requiredType)
Return an appropriate request object of the specified type, if available, unwrapping the given request as far as necessary.- 参数:
request
- the portlet request to introspectrequiredType
- the desired type of request object- 返回:
- the matching request object, or
null
if none of that type is available
getNativeResponse
public static <T> T getNativeResponse(PortletResponse response, Class<T> requiredType)
Return an appropriate response object of the specified type, if available, unwrapping the given response as far as necessary.- 参数:
response
- the portlet response to introspectrequiredType
- the desired type of response object- 返回:
- the matching response object, or
null
if none of that type is available
exposeRequestAttributes
public static void exposeRequestAttributes(PortletRequest request, Map<String,?> attributes)
Expose the given Map as request attributes, using the keys as attribute names and the values as corresponding attribute values. Keys must be Strings.- 参数:
request
- current portlet requestattributes
- the attributes Map
getCookie
public static Cookie getCookie(PortletRequest request, String name)
Retrieve the first cookie with the given name. Note that multiple cookies can have the same name but different paths or domains.- 参数:
request
- current portlet requestname
- cookie name- 返回:
- the first cookie with the given name, or
null
if none is found
hasSubmitParameter
public static boolean hasSubmitParameter(PortletRequest request, String name)
Check if a specific input type="submit" parameter was sent in the request, either via a button (directly with name) or via an image (name + ".x" or name + ".y").- 参数:
request
- current portlet requestname
- name of the parameter- 返回:
- if the parameter was sent
- 另请参阅:
WebUtils.SUBMIT_IMAGE_SUFFIXES
getSubmitParameter
public static String getSubmitParameter(PortletRequest request, String name)
Return the full name of a specific input type="submit" parameter if it was sent in the request, either via a button (directly with name) or via an image (name + ".x" or name + ".y").- 参数:
request
- current portlet requestname
- name of the parameter- 返回:
- the actual parameter name with suffix if needed - null if not present
- 另请参阅:
WebUtils.SUBMIT_IMAGE_SUFFIXES
getParametersStartingWith
public static Map<String,Object> getParametersStartingWith(PortletRequest request, String prefix)
Return a map containing all parameters with the given prefix. Maps single values to String and multiple values to String array.For example, with a prefix of "spring_", "spring_param1" and "spring_param2" result in a Map with "param1" and "param2" as keys.
Similar to portlet
PortletRequest.getParameterMap()
, but more flexible.- 参数:
request
- portlet request in which to look for parametersprefix
- the beginning of parameter names (if this isnull
or the empty string, all parameters will match)- 返回:
- map containing request parameters without the prefix, containing either a String or a String array as values
- 另请参阅:
PortletRequest.getParameterNames()
,PortletRequest.getParameterValues(java.lang.String)
,PortletRequest.getParameterMap()
getTargetPage
public static int getTargetPage(PortletRequest request, String paramPrefix, int currentPage)
Return the target page specified in the request.- 参数:
request
- current portlet requestparamPrefix
- the parameter prefix to check for (e.g. "_target" for parameters like "_target1" or "_target2")currentPage
- the current page, to be returned as fallback if no target page specified- 返回:
- the page specified in the request, or current page if not found
passAllParametersToRenderPhase
public static void passAllParametersToRenderPhase(ActionRequest request, ActionResponse response)
Pass all the action request parameters to the render phase by putting them into the action response object. This may not be called when the action will callsendRedirect
.- 参数:
request
- the current action requestresponse
- the current action response- 另请参阅:
StateAwareResponse.setRenderParameter(java.lang.String, java.lang.String)
clearAllRenderParameters
public static void clearAllRenderParameters(ActionResponse response)
Clear all the render parameters from theActionResponse
. This may not be called when the action will callsendRedirect
.- 参数:
response
- the current action response- 另请参阅:
StateAwareResponse.setRenderParameters(java.util.Map<java.lang.String, java.lang.String[]>)
serveResource
public static void serveResource(ResourceRequest request, ResourceResponse response, PortletContext context) throws PortletException, IOException
Serve the resource as specified in the given request to the given response, using the PortletContext's request dispatcher.This is roughly equivalent to Portlet 2.0 GenericPortlet.
- 参数:
request
- the current resource requestresponse
- the current resource responsecontext
- the current Portlet's PortletContext- 抛出:
PortletException
- propagated from Portlet API's forward methodIOException
- propagated from Portlet API's forward method