Portlet Preferences Example Program:

Portlet Preferences Example Program:
package com.prf;
import java.io.IOException;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.liferay.portal.*;
import com.liferay.portlet.*;
import javax.portlet.ActionRequest;

/**
 * Portlet implementation class PrfPortlet
 */
public class PrfPortlet extends MVCPortlet {
     
public void changePref(ActionRequest req,ActionResponse res) throws IOException,PortletException, SystemException
{
      String dname=req.getParameter("department");
      //getting portlet resource
      System.out.println(dname);
      String portletResource=ParamUtil.getString(req,"PortletResource");
      PortletPreferences pre=req.getPreferences();
      pre.setValue("deptname", dname);
      pre.store();
      String str=pre.getValue("deptname","");
      System.out.println(str);
     
}

}
Portlet.xml:
<?xml version="1.0"?>

      <portlet>
            <portlet-name>prf</portlet-name>
            <display-name>Prf Portlet</display-name>
            <portlet-class>com.prf.PrfPortlet</portlet-class>
            <init-param>
                  <name>view-jsp</name>
                  <value>/html/prf/view.jsp</value>
            </init-param>
            <init-param>
                  <name>edit-jsp</name>
                  <value>/html/prf/edit.jsp</value>
            </init-param>
            <expiration-cache>0</expiration-cache>
            <supports>
                  <mime-type>text/html</mime-type>
                  <portlet-mode>view</portlet-mode>
                  <portlet-mode>edit</portlet-mode>
            </supports>
            <resource-bundle>content/Language</resource-bundle>
            <portlet-info>
                  <title>Prf Portlet</title>
                  <short-title>Prf Portlet</short-title>
                  <keywords></keywords>
            </portlet-info>

            <portlet-preferences>
            <preference>
            <name>deptname</name>
            <value>computers</value>
            </preference>
            <preference>
            <name>company</name>
            <value>vidyayug</value>
            </preference>
            </portlet-preferences>
           
            <security-role-ref>
                  <role-name>administrator</role-name>
            </security-role-ref>
            <security-role-ref>
                  <role-name>guest</role-name>
            </security-role-ref>
            <security-role-ref>
                  <role-name>power-user</role-name>
            </security-role-ref>
            <security-role-ref>
                  <role-name>user</role-name>
            </security-role-ref>
      </portlet>
</portlet-app>









View.jsp:
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>
<%@page import="javax.portlet.PortletPreferences"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>]
<%@page import="javax.portlet.PortletRequest"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<%@page import="javax.portlet.PortletContext"%>
<%@page import="javax.portlet.ActionRequest"%>
<portlet:defineObjects />
<%
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
   preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}
 String department = preferences.getValue("deptname","");

 %>
 <%=department %>
<portlet:renderURL  var="modifypreference">
<portlet:param name="jspPage" value="/html/prf/modifypreference.jsp"></portlet:param>
</portlet:renderURL>
<a href="<%=modifypreference%>"> modify the preference</a>
Modify.jsp
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="javax.portlet.RenderResponse"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>
<%@page import="javax.xml.validation.Validator"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<%@page import="javax.portlet.PortletPreferences"%>
<%@page import="javax.portlet.RenderRequest"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<%
PortletPreferences preferences = PortalUtil.getPreferences(request);
String portletResource = ParamUtil.getString(request, "portletResource");
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
String department = preferences.getValue("deptname","");
%>
<%=department%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@page import ="javax.portlet.PortletURL" %>
<%@ page import="javax.portlet.RenderResponse" %>
<%@ page import="javax.portlet.ActionRequest" %>

<%

PortletURL changePrefURL=renderResponse.createActionURL();

changePrefURL.setParameter(ActionRequest.ACTION_NAME,"changePref");

%>

<center>
<form action="<%=changePrefURL.toString()%>" 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="click" />
</form>
</center>



In the above example what I have done is first i set the portlet preferences in portlet.xml and next I have read that preferences in view.jsp next I have choose the form in that I passed the preference deptname dynamically  and I was set the preference in portlet class method.

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.