Portlet preferences are used to store predefined preferences to a particular portlet. These are simple key-value pairs. Portlet preferences can be added to a particular portlet by using the Portlet.xml configuration file or dynamically by java code. Check out basic portlet life cycles before continuing.

  • Portlet preferences can be of two types:
    Read-only: By setting <read-only> parameter to true in portlet.xml file, we can make a preference read only.
  • Modifiable: Preference can be edited in Java code or can implement the portlet’s EDIT mode to provide the user an option to add his preferences.

Here is an example of adding Portlet Preferences, by implementing the EDIT mode of the portlet. This provides the user with an option to update preference.

<supports>
     <mime-type>text/html</mime-type>
     <portlet-mode>edit</portlet-mode>
</supports>
  • Also mention edit.jsp page under <init-parameters> as shown below:
<init-param>
      <name>edit-template</name>
      <value>/edit.jsp</value>
</init-param>
  • As mentioned earlier, we can add Portlet preferences using the portlet.xml file and also make them read only.
<portlet-preferences>
  <preference>
     <name>readOnly</name>
     <value>Read Only Value</value>
     <read-only>true</read-only>
  </preference>
  <preference>
     <name>modifyable</name>
     <value>Initial Value</value>
  </preference>
</portlet-preferences>
  • To implement EDIT mode we have to override doEdit() method of GenericPortlet class as shown below. Here we are rendering edit.jsp file, whenever the edit mode of portlet is invoked.
protected void doEdit(RenderRequest renderRequest,
RenderResponse renderResponse) throws PortletException, IOException
{
    renderResponse.setContentType("text/html");
    include(editJSP, renderRequest, renderResponse);
}
  • Users can use the portlet configuration tab to change the mode of the portlet to EDIT as shown in the following image. Users can add Preferences by clicking the “Preferences” tab.
Preference1
  • In the edit.jsp add the following code to add the preference value as shown below:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL var="processAction"/>
<h3>Add new Portlet Preference</h3>
<form action="<%=processAction %>" method="post">
   <h4>Add new Value:</h4><br/>
   <input name="<portlet:namespace/>newPreference" type="text" /><br/>
   <input name="name" type="submit" value="Submit" />
</form>
  • Implement the action class to store the user entered preference value.
public void processAction(ActionRequest actionRequest, 
ActionResponse actionResponse)throws PortletException, IOException
{
      PortletPreferences prefs = actionRequest.getPreferences();
      String newParameter = actionRequest.getParameter("newPreference");
 
      prefs.setValue("editablePref", newParameter);
      prefs.store();
 
      actionResponse.setPortletMode(PortletMode.VIEW);
}
  • Finally, inside doView() method, get the preferences from renderRequest and set the renderRequest attribute to display them on view.jsp file as shown. We are also getting preferences, which were added in portlet.xml file above:
public void doView(RenderRequest renderRequest, 
RenderResponse renderResponse) throws IOException, PortletException 
{
     PortletPreferences prefs = renderRequest.getPreferences();
 
     renderRequest.setAttribute("modifyable", 
                  prefs.getValue("modifyable","No value" )); 
     renderRequest.setAttribute("readOnly", 
                  prefs.getValue("readOnly","No value" )); 
     renderRequest.setAttribute("editablePref", 
                  prefs.getValue("editablePref","No value" )); 
 
     include(viewJSP, renderRequest, renderResponse);
}
  • Add the view.jsp with following content to display stored preferences:
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<%
    PortletPreferences prefs = renderRequest.getPreferences();

    String modifyable = (String) renderRequest.getAttribute("modifyable");
    String readOnly = (String) renderRequest.getAttribute("readOnly");
    String editablePref = (String) renderRequest
.getAttribute("editablePref");
%>
 <h3>Existing preference: <br/></h3>
 Editable pref: <%=editablePref %><br/>
 ReadOnly pref: <%=readOnly %><br/>
 Modifyable pref: <%=modifyable %><br/>
  • Finally, the action class will look like this:

TestAction.java:

package com.test;
import com.liferay.portal.kernel.util.Validator;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletPreferences;

public class TestAction extends GenericPortlet
{
   public void init()
   {
     viewJSP = getInitParameter("view-template");
     editJSP = getInitParameter("edit-template");
   }
 
  public void doView(RenderRequest renderRequest, 
RenderResponse renderResponse) throws IOException, PortletException 
   {
     PortletPreferences prefs = renderRequest.getPreferences();
 
     renderRequest.setAttribute("modifyable", prefs
            .getValue("modifyable","No value" )); 
     renderRequest.setAttribute("readOnly", prefs
            .getValue("readOnly","No value" )); 
     renderRequest.setAttribute("editablePref", prefs
            .getValue("editablePref","No value" )); 
 
     include(viewJSP, renderRequest, renderResponse);
   }
 
 public void processAction(ActionRequest actionRequest, 
ActionResponse actionResponse)throws PortletException, IOException
 {
     PortletPreferences prefs = actionRequest.getPreferences();
     String newParameter = actionRequest.getParameter("newPreference");
 
     prefs.setValue("editablePref", newParameter);
     prefs.store();
 
     actionResponse.setPortletMode(PortletMode.VIEW);
 }
 
 protected void doEdit(RenderRequest renderRequest
         , RenderResponse renderResponse)throws PortletException, 
IOException
 {
     renderResponse.setContentType("text/html");
     include(editJSP, renderRequest, renderResponse);
 }
 
 public void include(String path, RenderRequest renderRequest, 
    RenderResponse renderResponse) throws PortletException, IOException
 {
       PortletRequestDispatcher dispatcher = getPortletContext()
                 .getRequestDispatcher(path);
     if(!(dispatcher == null))
     {
         dispatcher.forward(renderRequest, renderResponse);
     }
 }
   static String viewJSP;
   static String editJSP;
}
  • After saving the preference through EDIT mode, the result page will look like this:
Preference2

Note: Version details: Liferay version 6.2 + Apache Tomcat 7 server.

You may also interested in