Share

Liferay Configurable Portlet Preferences

In this section we will see how to do portlet preferences.

Portlet preferences are useful for Administrators and Users.

          Administrators it is useful for customized views of portlet to subset of users or all of the users.

          Users access themselves for configuring a portlet just the way they like it.

Lets dive into portlet preferences by running through an example of creating configuration page for portlet.

Steps to create preferences page
  1. Specify a configuration Jsp page in portlet.xml
  2. Create configuration Jsp for displaying the portlet preferences options
  3. Create configuration action implementation class for processing the portlet preference value
  4. Modify the View jsp to respond to the current Portlet Preference Value.
Before doing above steps pre requsite step is create default Setup tab in the Portlet's Configuration page.

Creating the default configuration page as follows

Open the liferay-portlet.xml and add the element 
<configuration-action-class>
            com.liferay.portal.kernel.portlet.DefaultConfigurationAction</configuration-action-class> 
below <icon>....</icon> tag .

Create one portlet which is  having the service layer also.

Step 1: Specify a configuration Jsp page in portlet.xml
  • We will create a configuration jsp in docroot/library/html/configuration.jsp
  • Configure that jsp as a init param has name as config-template and value as path of jsp in portlet.xml as follows.
               <init-param>
                       <name>config-template</name>
                       <value>/html/library/configuration.jsp</value>
               </init-param>

Step 2: Create configuration Jsp for displaying the portlet preferences options
Now lets begin creating our portlet preference for configuration page preferences content.Add the following code to the configuration.jsp
<%@include file="init.jsp" %>

<liferay-portlet:actionURL portletConfiguration="true" var="configurationURL" />

<%
boolean showAuthor_cfg = GetterUtil.getBoolean(portletPreferences.getValue("showAuthor", StringPool.TRUE));
%>

<aui:form action="<%= configurationURL %>" method="post" name="fm">
        <aui:input name="showAuthor" type="checkbox" value="<%= showAuthor_cfg %>" />
        <aui:button-row>
                      <aui:button type="submit" />
        </aui:button-row>
</aui:form>

In your init.jsp add the following code

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %><%@
taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %><%@
taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %><%@
taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %><%@
taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %><%@
taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@ page import="com.liferay.portal.kernel.util.StringPool" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />

The taglib directives access the JSTL,Liferay taglib and theme taglib.<portlet:defineObjects/> tag to access implicit varibles like renderURL .

Step 3: Create configuration action implementation class for processing the portlet preference                value.
Now lets create custom configuration action class for accessing the  portlet preference and extend DefaultConfigurationAction class.
  • Create one package com.laxman.preferences in the portlet's docroot/WEB-INF/src directory.
  • Create a class named ConfigurationActionImpl and extends DefaultConfigurationAction as its super class.
package com.laxman.preferences;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;

public class ConfigurationActionImpl extends DefaultConfigurationAction {

    @Override
    public void processAction(
        PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse) throws Exception {

        super.processAction(portletConfig, actionRequest, actionResponse);

        PortletPreferences prefs = actionRequest.getPreferences();

        String showLocationAddress = prefs.getValue(
            "showAuthor", "true");
    }
}


Let's specify our new custom configuration class in the liferay-portlet.xml.Replace the existing <configuration-action-class>....</configuration-action-class> with
<configuration-action-class>com.laxman.preferences.ConfigurationActionImpl</configuration-action-class>
.
Step 4: Modify the  view.jsp to respond to the current Portlet Preference Value.

Create a view_all_student.jsp in docroot/html/library/view_all_student.jsp and add the following code.

<%@page import="com.library.slayer.service.LMSBookLocalServiceUtil"%>
<%@page import="javax.portlet.PortletURL"%>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ include file="init.jsp"%>
<%
boolean showAuthor_view = GetterUtil.getBoolean(portletPreferences.getValue("showAuthor", StringPool.TRUE));
%>
<liferay-portlet:renderURL varImpl="iteratorURL">
           <portlet:param name="mvcPath" value="/html/library/view_all_student.jsp" />
</liferay-portlet:renderURL>
<a href="<portlet:renderURL />">&laquo;Home</a>
<div class="separator"></div>
<liferay-ui:search-container emptyResultsMessage="there-are-no-students"
                                headerNames="bookId,bookTitle,author,dateAdded"
                                iteratorURL="<%=iteratorURL %>"
                                delta="5"
                                deltaConfigurable="true"
                                total="<%=LMSBookLocalServiceUtil.getLMSBooksCount()%>"
                             >
            <liferay-ui:search-container-results results="                                                                                                                       <%=LMSBookLocalServiceUtil.
                                        getLMSBooks(searchContainer.getStart(), searchContainer.getEnd()) %>"/>
           <liferay-ui:search-container-row className="com.library.slayer.model.LMSBook"
                                keyProperty="bookId" modelVar="currentStudent">
            <liferay-portlet:renderURL varImpl="rowURL">
                     <portlet:param name="backURL" value="<%=currentUrl %>" />
                     <portlet:param name="mvcPath"   value="/html/library/display_book.jsp" />
                     <portlet:param name="bookId"      value="<%=                                                                                                                                              String.valueOf(currentStudent.getBookId()) %>" />
            </liferay-portlet:renderURL>
           <liferay-ui:search-container-column-text href="<%= rowURL %>"
                                                                                          name="bookId" property="bookId" />

            <liferay-ui:search-container-column-text href="<%= rowURL %>"
                                                                                   name="bookTitle" property="bookTitle" />

           <c:choose>
                          <!--Based on the preference value it will show this column-->
                          <c:when test="<%= showAuthor_view ==true %>">
                                        <liferay-ui:search-container-column-text href="<%= rowURL %>"
                                                                            name="author" property="author" />
                         </c:when>
           </c:choose>
           </liferay-ui:search-container-row>
           <liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
</liferay-ui:search-container>


Add the following code snippet to the view.jsp

<%@ include file="view_all_student.jsp"%>

No comments:

Post a Comment