Thursday, April 16, 2015

Portlet Preferences In Liferay 6.2


There are two types of portlet preference available in liferay

I>Static Portlet Preferences
II>Dynamic Portlet Preferences(Run time preferences)

I. STATIC PORTLET PREFERENCE
   --------------------------------------------

In static portlet preference, prefrences which we set will be constant throughout the portlet.

Step 1: Create one portlet plugin project name TestingPortletPreference inside this plugin project create one portlet named StaticPreferences

Step 2: Define below preferences after </portlet-info> tag in portlet.xml


          
             Blogger
             Liferay
          
                  
             Blog
             thinkliferayjava.blogspot.in
          


Step 3: For getting preference value in jsp we need portletpreference object and as per JSR- 286 specification portletpreference is a implicit
object and it is available in jsp by using <portlet:defineObjects />
tag.
<%@page import="javax.portlet.PortletPreferences"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



Static Portlet Preferences

<% portletPreferences = renderRequest.getPreferences(); String blogger = portletPreferences.getValue("Blogger", ""); String blog = portletPreferences.getValue("Blog", ""); %> Blogger :

<%=blogger%>


Blog :

<%=blog%>


*We can create portlet preference object like this also.
 PortletPreferences preference = renderRequest.getPreferences();

Output
--------


II. DYNAMIC PREFERENCE
    ----------------------------------
 
 In dynamic preference we can change the portlet preferences value at runtime and we can access the value throughout the portlet.
 The preferences for a portlet are stored in "PortletPreferences" table.

 Step 1. Create one portlet plugin project name TestingPortletPreference and inside that create one portlet name DynamicPreferences

 Step 2. First we set preference value using portletpreference object in portletpreference table then we reset that value several times.
 <%@page import="javax.portlet.PortletURL"%>
<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="javax.portlet.PortletPreferences"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



 <%
   PortletURL resetPreferenceURL = renderResponse.createActionURL();
   resetPreferenceURL.setParameter(actionRequest.ACTION_NAME, "resetPreference");

   portletPreferences = renderRequest.getPreferences();
   
   String blogger = portletPreferences.getValue("Blogger","");
   String blog = portletPreferences.getValue("Blog", "");
 %>

  
Blogger:

<%=blogger%>

Blog :

<%=blog%>

Blogger: Blog:

Step 3: Paste below code in java class DynamicPreferences.java

package com.test;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class DynamicPreferences
 */
public class DynamicPreferences extends MVCPortlet {


    public void resetPreference(ActionRequest actionRequest,
            ActionResponse actionResponse) throws IOException, PortletException {

         String blogger= ParamUtil.getString(actionRequest, "Blogger");
         String blog = ParamUtil.getString(actionRequest, "Blog");
        
         PortletPreferences preference=actionRequest.getPreferences();
         
         preference.setValue("Blogger", blogger);
         preference.setValue("Blog", blog);
         preference.store();
         
         
         //For testing, preference is reset or not
         String resetPreferenceBlogger = preference.getValue("Blogger","");
         String resetPreferenceBlog = preference.getValue("Blog","");
        
         System.out.println(resetPreferenceBlogger);
         System.out.println(resetPreferenceBlog);
    }
    
    
}

*If we want to set some default value before resetting then we can set default value using portlet preferences tags inside portlet.xml
we have done in static preference.

0 comments:

Post a Comment