The session attribute plug-in
Use the SessionAttribute rendering plug-in to retrieve, set, or remove attributes from the portlet or servlet session. When we access the portlet session, we can also specify the scope of the session attributes as either application or portlet scope.
Syntax:
[Plugin:SessionAttribute key="" value="" mode="" scope="" defaultValue="" escape=""]
Attributes:
- scope
- Specify the session and attribute scope with which to work:
- application
- Access the portlet session in portlet application scope.
- portlet
- Access the portlet session in portlet scope. This scope is the default scope.
- servlet
- Access the servlet session.
- key
- Specify the name of the session attribute to retrieve, set, or remove.
- mode
- Specify the operation to perform:
- get
- Retrieve the specified attribute from the session and return the attribute value.
- set
- Set the specified attribute to the session.
- delete
- Delete the specified attribute from the session.
- remove
- Remove the specified attribute from the session and return the attribute value that was previously set.
- value
- Specify the value of the attribute to set. If an attribute with the same name exists on the request, that attribute is overwritten with the new value. If we want to set an attribute on the request, this attribute is mandatory.
- defaultValue
- Specify the value to be to returned if the addressed session parameter does not exist or has a value of null.
- escape
- Use this attribute to define the escaping to be used for writing the URL. Specify one of the values xml, json, javascript, or none. The default value is none. This value is evaluated only if the mode attribute is set to get or remove.
Examples
- To retrieve the attribute with the key key1 from the portlet session in portlet scope, use either one of the following plug-in tags:
[Plugin:SessionAttribute key="key1"] [Plugin:SessionAttribute scope="portlet" key="key1"]
- To retrieve the attribute with the key key1 from the portlet session in portlet application scope, use the following plug-in tag:
[Plugin:SessionAttribute scope="application" key="key1"]
- To retrieve the attribute with the key key1 from the servlet session and to return the value defaultValue if the attribute is not set or has a value of null, use the following plug-in tag:
[Plugin:SessionAttribute scope="servlet" key="key1" defaultValue="defaultValue"]
- To set the attribute with the key key1 and the value value1 to the portlet session in portlet session scope, use one of the following plug-in tags:
[Plugin:SessionAttribute key="key1" value="value1"] [Plugin:SessionAttribute mode="set" key="key1" value="value1"]
- To delete the attribute with the key key1 from the portlet session in portlet application scope without returning the previous value, either use the delete mode or pass an empty value:
[Plugin:SessionAttribute key="key1" mode="delete"] [Plugin:SessionAttribute key="key1" value=""]
- To remove the attribute with the key key1 from the portlet session in portlet application scope and to return the previously set value, use the following plug-in tag:
[Plugin:SessionAttribute scope="application" key="key1" mode="remove"]