Saturday, April 18, 2015

Dynamic Preference Using Portlet Edit Mode.



Assume we want to display day of weeks in  portlet and want to change everyday, we can achieve this requirement using dynamic preference using
Edit mode.

Step 1: Create one portlet plugin project with name TestingPortletPreference, inside this project create one portlet with name
DynamicPreferences.

Step 2: Enable portlet edit mode in portlet.xml as shown below
 


<?xml version="1.0"?>



<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">

    



    

    <portlet>

        <portlet-name>dynamic-preferences</portlet-name>

        <display-name>Dynamic Preferences</display-name>

        <portlet-class>com.test.DynamicPreferences</portlet-class>

        <init-param>

            <name>view-template</name>

            <value>/html/dynamicpreferences/view.jsp</value>

        </init-param>

        

        <init-param>

                <name>edit-template</name>

                <value>/html/dynamicpreferences/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>

        <portlet-info>

            <title>Dynamic Preferences</title>

            <short-title>Dynamic Preferences</short-title>

            <keywords></keywords>

        </portlet-info>

        <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>



Step 3: create one jsp page name edit.jsp inside /docroot/html/dynamicpreferences  and paste below code in edit.jsp

 
<%@page import="javax.portlet.PortletMode"%>

<%@page import="javax.portlet.PortletURL"%>

<%@page import="com.liferay.portal.kernel.util.StringPool"%>

<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>

<%@page import="com.liferay.portal.kernel.util.Validator"%>

<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>

<%@page import="javax.portlet.PortletPreferences"%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />



<%

PortletURL  savePreferensesRenderURL = renderResponse.createRenderURL();

savePreferensesRenderURL.setPortletMode(PortletMode.EDIT);



 portletPreferences = renderRequest.getPreferences();



String weekDay = portletPreferences.getValue("weekDay",null);



%>

<p style="border:1px solid green;"> Add/Edit Portlet Preferenses</p>



<form action="<%=savePreferensesRenderURL%>" name="savePreferenses"  method="POST">

   <input  type="text" name="<portlet:namespace/>weekDay" value="<%=weekDay %>" /><br/>

   <input type="submit" value="Set/Reset Preferenses"/>

</form>



Step 4: paste below code in portlet view.jsp


 
<%@page import="java.util.Date"%>

<%@page import="java.text.SimpleDateFormat"%>

<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>

<%@page import="com.liferay.portal.kernel.util.Validator"%>

<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>

<%@page import="javax.portlet.PortletPreferences"%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



<portlet:defineObjects />



<%

PortletPreferences preferences = renderRequest.getPreferences();



String weekDay = preferences.getValue("weekDay",null);



%>

Today is : <%=weekDay%>




Step 5: Paste below code in portlet controller java class DynamicPreferences.java

 
package com.test;



import java.io.IOException;



import javax.portlet.PortletException;

import javax.portlet.PortletPreferences;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;



import com.liferay.portal.kernel.util.ParamUtil;

import com.liferay.util.bridges.mvc.MVCPortlet;



/**

 * Portlet implementation class DynamicPreferences

 */

public class DynamicPreferences extends MVCPortlet {





    @Override

    public void doEdit(RenderRequest renderRequest,

               RenderResponse renderResponse) throws IOException, PortletException {

          

        String weekDay = ParamUtil.getString(renderRequest, "weekDay");

        

          System.out.println("====" + weekDay);

          

          PortletPreferences preferences = renderRequest.getPreferences();

          

          preferences.setValue("weekDay", weekDay);

          preferences.store();

          super.doEdit(renderRequest, renderResponse);

         

    }

}

Related Posts:

  • Liferay Friendly Url I>Friendly url when we access jsp pages(rendering from one jsp to another) II>Friendly Url when we call action url III> Friendly Url when … Read More
  • Sharding in Liferay Sharding in Liferay A Database shard is a Horizontal partitioning of databases. Each individual partition is referred as shard or database sha… Read More
  • Email Configuration with liferay Step 1: Create one portlet plugin project with name MailingConfiguration , inside this plugin create one portlet name MailConfig Step 2: Paste belo… Read More
  • Sign in using Facebook in liferay When user don't want to create account in liferay portal but want to to login then user can use the sign in using many social platform like Fa… Read More
  • Portlet Permission In Liferay Level 1 Security     Assume there is one portlet in which admin can assign permission to to  user role who can see restricted p… Read More

0 comments:

Post a Comment