17 February, 2011

Portlet Preferences

There are two kinds of portlet preferences available.
  • Static Preferences
  • RunTime Preferences
  •   
1. Static Preferences:

It is nothing but, the portlet prefrences which we set will be constant troughout the portlet.
Example:
Mention the following lines inside your portlet.xml file:
<portlet-preferences>
 <preference>
      <name>department</name>
      <value>liferay</value>
   </preference>


    <preference>        <name>company</name>
      <value>cignex</value>
   </preference>

</portlet-preferences>

Access the above preferences department and company in your jsp or java files.
PortletPreferences preferences = renderRequest.getPreferences();
String department = preferences.getValue("departmentt", "");
String company = preferences.getValue("company", "");


2. RunTime Preferences:

We can change the portlet preferences value at runtime and we can access the value throught the portlet. The preferences for a portlet are stored in "PortletPreferences" table.
Steps to follow:

  • Access the "department" value in jsp page from the portlet preferences table.
  • Provide another jsp page to modify the department value.
  • Update the department value in portlet preferences table.
Example:

Step 1: Mention the following code where you want to access department value. In our case we using view.jsp page.

<%
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
   preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);

}
 String department = preferences.getValue("department", "liferay");
%>
Note: If the department value is null while fetching from portlet preferences table, by default liferay will be assigned to department value, because we mentioned liferay will be the default value for department.
Step 2: Mention the following code where you want to provide the UI for altering the department value. In our case we using modifyportletprefernces.jsp page.

<%
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
  preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);

}
String department = preferences.getValue("department", "liferay");
%>
<form action="Mention your action" method="POST" name="<portlet:namespace />fm">
  <table>
    <tr>
      <td class="lfr-label">Page Title</td>
      <td><input name="<portlet:namespace />department" type="text" value="<%= department%>"/></td>
    </tr>
  </table>
  <input type="submit" value="<liferay-ui:message key="save" />" />
</form>

Step 3: Mention the following code in your action class to update the department value in the portlet preferences table.

String depratment= ParamUtil.getString(actionRequest, "department");
//Getting the PortletResource
String portletResource = ParamUtil.getString(actionRequest, "portletResource");

//Creating the Reference for Portletpreferences Table
PortletPreferences prefs = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);

 //Setting The Value in the PortletPreferences table
prefs.setValue("department", department);
prefs.store();

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...