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); } }
0 comments:
Post a Comment