Introduction:
We all know that we can use sessions to store attributes, but Liferay PortletSession is somewhat special. It can use different scopes to store different attributes. Our sample code is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15061KS6-0.png "title =" 1.png" alt = "153666521.png"/>
Here we will study the details of PortletSession, for example, how to store the attributes on different scopes.
Debugging Analysis:
For the first case: session. getAttribute ("foo", PortletSession. PORTAL_SCOPE), it actually calls the following branch of the getAttribute () method of PortletSessionImpl:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15061L3I-1.png "title =" 2.png" alt = "154430719.png"/>
In fact, from a macro perspective, it still obtains an attribute from httpSession (), but the key of this attribute is no longer the original key, but an encapsulation value, the encapsulated value is obtained through _ getPortletScopeName (name. Let's take a look at the details of this method and it will call_ GetPortletScopeName (_ portletName, _ plid, name)Method, that is, the generated key is jointly determined by portletName, portletLayoutId, and the key of the required attribute:
Specifically, its call can be seen as follows:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/154830287.png "title =" 3.png" alt = "154830287.png"/>
It will be followed after calling getPortletScope (portletName, plid? Name to obtain the final key.
The call to getPortletScope (portletName, plid) is very simple, as follows:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/155031834.png "title =" 4.png" alt = "155031834.png"/>
It will attach Portlet_SCOPE_NAMESPACE (javax. portlet. p.), portletName, _ LAYOUT _, and plid.
Because the above parameters are known, it is easy to get the returned value of getPortletScope (portletName, plid:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15061I306-4.png "title =" 5.png" alt = "155337795.png"/>
In this way, connect the question mark? And the original key of the attribute. Later, you will soon be able to get the key that is finally stored on httpSession.
The value isJavax. portlet. p. logsearchportlet_WAR_logsearchportlet_LAYOUT_21601? Foo
In the second case,Session. getAttribute ("bar", PortletSession. APPLICATION_SCOPE), which actually calls the following branch of the getAttribute () method of HttpSession:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15061J509-5.png "title =" 7.png" alt = "161007833.png"/>
Therefore, it is straightforward to get the corresponding attribute value from httpSession Based on the passed attribute key.
Summary:
Through this simple small experiment, we can see the following conclusions:
(1)The variables obtained on PortletSession are essentially no different in PORTLET_SCOPE and APPLICATION_SCOPE. They are all obtained from HttpSession, except that the keys of variables are different. For PORTLET_SCOPE, this key adds many strings before the original key as the final key. For APPLICATION_SCOPE, this key is the final key.
(2) For PORTLET_SCOPE, the key string mode of the final attribute is Portlet namespace + portletName + (_ LAYOUT _) + portletLayoutId + "? "+ Original keyPortlet namespace. Therefore, it is determined by portletName, portletLayoutId, and original key. We can also extend this conclusion. Because the portletName of different portlet must be different, and the key must be different, PortletSession cannot share the attributes of other portlet on PORTLET_SCOPE, even if these portlet are in the same session. If the same portlet is deployed multiple times, layoutId must be different, and the key must be different. Therefore, PortletSession cannot share this portlet after multiple deployments, attributes stored on PORTLET_SCOPE in previous instances, even if these portlet instances are in the same session. However, attributes can still be shared through APPLICATION_SCOPE for multiple portlet or single-portlet instances in the same session.
(3) because PortletSession can transform the attribute key to distinguish attributes on APPLICATION_SCOPE and PORTLET_SCOPE, PortletSession is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15061I315-6.png "title =" 6.png" alt = "160723163.png"/>
This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1297791