To store and share settings within the client browser, use session preferences.
The Master-Detail design pattern illustrates the most basic usage of session preferences. This design pattern splits control and display between two portlets. For example, the "master" portlet could summarize data in list form, and the "detail" portlet could display details on each data item in response to user selection. In the example below, the master portlet displays a form that allows you to enter a color code in a text box.
When the user enters a color code in the text box, the color in the detail portlet changes.
ALI Scripting Framework Methods
This example is oversimplified; the master portlet makes a direct call to a JavaScript method of the detail portlet. Unless the master portlet takes extra measures to ensure that the detail portlet is actually present on the same page, calls from master to detail could generate errors. The ALI Scripting Framework provides an easy way to detach the relationship between portlets and use a common event interface for communication. See Using ALI Scripting Framework Event Notification for more information on inter-portlet communication.Master Portlet
<div style="padding:10px;" align="center"> <p><b>Enter color:</b> <input type="text" style="font-size:22px;font-weight:bold;text-align:center;" id="master_prefName" value="#FFFFFF" size="8" onkeyup="master_setPrefs(this.value)"></p><br> </div> <script type="text/javascript"> function master_setPrefs(val) { var prefName = 'masterColor'; var prefValue = val; PTPortlet.setSessionPref(prefName,prefValue); master_debug('<b>Master Portlet</b> called PTPortlet.setSessionPref(\'masterColor\',\'' + prefValue + '\').'); if (window.detail_update) { master_debug('<b>Master Portlet</b> calling detail_update().'); detail_update(); } else { master_debug('Could not locate portlet <b>Detail Portlet</b> on page.'); } } function master_debug(str) { if (window.PTDebugUtil) { PTDebugUtil.debug(str); } } </script>
Detail Portlet
<div style="padding:10px;" align="center"> <p><b>Color swatch</b> <div style="width:100px;height:100px;border:2px solid black;padding:2px;"id="detail-swatch"></div> <script> function detail_update() { var color = PTPortlet.getSessionPref('masterColor'); detail_debug('<b>Detail Portlet</b> received value="' + color + '" for PTPortlet.getSessionPref(\'masterColor\')'); var swatch = document.getElementById('detail-swatch'); if (swatch) { swatch.innerHTML = '<div style="background-color:' + color + ';width:100%;height:100%;"></div>'; } else { detail_debug('<b>Detail Portlet</b> cannot find \'detail-swatch\' DIV element.'); } } function detail_debug(str) { if (window.PTDebugUtil) { PTDebugUtil.debug(str); } } </script>
IDK Methods
In most cases, reading session preferences via the ALI Scripting Framework is inefficient and insecure. Always use the IDK to read session preferences, as shown in the example code below.Java
<%@ page language="java" import="com.plumtree.remote.portlet.*,java.util.Date" %> IPortletContext portletContext = PortletContextFactory.createPortletContext(request,response); IPortletResponse portletResponse = portletContext.getResponse(); IPortletUser portletUser = portletContext.getUser(); IPortletRequest portletRequest = portletContext.getRequest(); masterColor = portletRequest.getSettingValue(SettingType.Session, "masterColor");
.NET
... Dim portletContext As IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request, Response) Dim portletRequest As IPortletRequest portletRequest = PortletContext.GetRequest Dim portletUser As IPortletUser portletUser = PortletContext.GetUser Dim portletResponse As IPortletResponse portletResponse = PortletContext.GetResponse Dim masterColor As String masterColor = portletRequest.GetSettingValue(SettingType.Session "masterColor") ...