Liferay Portlet Configuration Page in Plugin Portlet

Liferay configuration page provides a way to store portlet related configurations.
Generally in the development we may need to store portlet specific configurations and we need one user interface page to input some values and store into database. These portlet specific configuration will be stored in portlet preferences table.

To input these values we will use portlet configuration page there we can design user interface to submit values and page can be access through portlet configuration icon that is at top right corner of portlet it can see visible when we drag and drop the portlet in the page.

Assume Scenario:

In our custom portlet we want to display Site specific users in the portlet. We will store Site Id in portlet preferences and we will take site Id to get site ,form the site we will fetch users to display it in the table.

to set these portlet configuration we will use portlet configuration page there user will change site so that user selected site will be stored in portlet preference.

Portlet Configuration Page Implementation Steps.
  1. Create configuration JSP page
  2. Implement Custom Configuration Action Class
  3. Configure Configuration Action Class in liferay-portlet.xml
Create configuration JSP page

First we need to create portlet configuration page and there use will input data.
Create form and create requited input elements.

In our case we only need select box that should display all sites form there user will select one option.

We should create configuration action URL using <liferay-portlet:actionURL/> tag and we should use "portletConfiguration" attribute value as “true”

Example Configuration Action URL

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

When we submit form using above URL then it will invoke Custom Configuration Action Class

Implement Custom Configuration Action Class

We need to implement Custom Configuration Action class there will capture portlet configurations and will stored as portlet preferences.

Our Custom class should implments the ConfigurationAction interface and in the render method we need to specify the configuration JSP page path that was created in the first step.

We have processAction(--) method there will write configuration values storing logic and render(---) method we simply specify the portlet configuration jsp page path. We will use portlet preference to store the configuration values.

Example Syntax:

public class PortletConfigurationAction implements ConfigurationAction {
public void processAction(PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
}
public String render(PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throws Exception {
return "/html/portletconfiguration/portlet_configuration.jsp";
}
}

Configure Configuration Action Class in “liferay-portlet.xml”

Finally we need to configure the Configuration Implementation java class in liferay-portlet.xml file. We need to give Java class with complete package where it is reside.

The following is tag to configure portlet configuration class

<configuration-action-class>
com.meera.portletconfig.PortletConfigurationAction
</configuration-action-class>

The following is complete code example for portlet configuration

Create liferay Plugin portlet using Liferay IDE and add following jsp pages and java classes then you can be ready with portlet configuration page.

init.jsp (/html/portletconfiguration/init.jsp)

<%@pageimport="com.liferay.portal.kernel.util.Validator"%>
<%@taglib uri="http://liferay.com/tld/aui"prefix="aui"%>
<%@tagliburi="http://liferay.com/tld/portlet"prefix="liferay-portlet"%>
<%@tagliburi="http://liferay.com/tld/security"prefix="liferay-security"%>
<%@tagliburi="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"%>
<%@tagliburi="http://java.sun.com/portlet_2_0"prefix="portlet"%>
<%@pageimport="com.liferay.portal.kernel.util.Constants"%>
<%@pageimport="com.liferay.portal.kernel.util.GetterUtil" %>
<%@pageimport="com.liferay.portal.kernel.util.ParamUtil"%>
<%@pageimport="com.liferay.portal.kernel.util.StringPool"%>
<%@pageimport="com.liferay.portal.util.PortalUtil"%>
<%@pageimport="com.liferay.portlet.PortletPreferencesFactoryUtil"%>
<%@pageimport="javax.portlet.PortletPreferences"%>
<%@page import="javax.portlet.WindowState"%>
<%@page import="javax.portlet.PortletURL"%>
<%@pageimport="javax.portlet.ActionRequest"%>
<%@pageimport="javax.portlet.PortletPreferences"%>
<%@pageimport="com.liferay.portal.kernel.language.LanguageUtil"%>
<%@pageimport="com.liferay.portal.security.permission.ActionKeys"%>
<liferay-theme:defineObjects/>
<portlet:defineObjects/>
<%
String currentURL = PortalUtil.getCurrentURL(request);
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if(Validator.isNotNull(portletResource)) {
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}
String siteId = preferences.getValue("siteId","1");
%>


portlet_configuration.jsp (/html/portletconfiguration/portlet_configuration.jsp)

<%@pageimport="com.liferay.portal.model.Group"%>
<%@pageimport="java.util.List"%>
<%@pageimport="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@pageimport="com.liferay.portal.service.GroupLocalService"%>
<%@pageimport="com.liferay.portal.kernel.util.Constants"%>
<%@includefile="init.jsp"%>
<%
List<Group> list=GroupLocalServiceUtil.getGroups(0,GroupLocalServiceUtil.getGroupsCount());
%>
<liferay-ui:successkey="potlet-config-saved"message="Portlet Configuration have been successfully saved" />
<liferay-portlet:actionURLvar="configurationActionURL"portletConfiguration="true"/>
<aui:formaction="<%=configurationActionURL%>"method="post"name="configurationForm">
Select Site:<br/>
<aui:selectname="siteId"id="siteId">
<%for(Group group:list) {%>
<aui:optionvalue="<%=group.getGroupId() %>"label="<%=group.getName() %>"></aui:option>
<%} %>
</aui:select>
<br/>
<aui:buttontype="submit"name="Save Configuration" value="Save Configuration"></aui:button>
</aui:form>

view.jsp(/html/portletconfiguration/view.jsp)

<%@page import="java.util.ArrayList"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.service.UserLocalService"%>
<%@include file="init.jsp"%>
<%
List<User> users=new ArrayList<User>();
long longSiteId = GetterUtil.getLong(siteId);
String SiteName="";
try{
users = UserLocalServiceUtil.getGroupUsers(longSiteId);
SiteName=GroupLocalServiceUtil.getGroup(longSiteId).getName();
}catch(Exception e){

}

%>
<h3>Selected Site "<%=SiteName%>" Users</h3>
<table border="1">
<tr>
<th>User First Name</th>
<th>User Last Name</th>
<th>User Screen Name</th>
<th>User Email Address</th>
</tr>
<%
for (User userobj : users) {
%>
<tr>
<td><%=userobj.getFirstName()%></td>
<td><%=userobj.getLastName()%></td>
<td><%=userobj.getScreenName()%></td>
<td><%=userobj.getEmailAddress()%></td>

</tr>
<%
}
%>
</table>

PortletConfigurationAction.java (com.meera.portletconfig.PortletConfigurationAction.java)

packagecom.meera.portletconfig;
importjavax.portlet.ActionRequest;
importjavax.portlet.ActionResponse;
importjavax.portlet.PortletConfig;
importjavax.portlet.PortletPreferences;
importjavax.portlet.RenderRequest;
importjavax.portlet.RenderResponse;
importcom.liferay.portal.kernel.portlet.ConfigurationAction;
importcom.liferay.portal.kernel.servlet.SessionMessages;
importcom.liferay.portal.kernel.util.ParamUtil;
importcom.liferay.portlet.PortletPreferencesFactoryUtil;
publicclassPortletConfigurationAction implementsConfigurationAction{
publicvoidprocessAction(PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse)
throwsException {
String siteId= ParamUtil.getString(actionRequest, "siteId");
String portletResource= ParamUtil.getString(actionRequest,"portletResource");
System.out.println("siteId"+siteId);
PortletPreferences preferences= PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);
preferences.setValue("siteId", siteId);
preferences.store();
SessionMessages.add(actionRequest,"potlet-config-saved");
}
publicString render(PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throwsException {
return"/html/portletconfiguration/portlet_configuration.jsp";
}
}

liferay-portlet.xml

<liferay-portlet-app>
<portlet>
<portlet-name>portlet-configuration-action</portlet-name>
<icon>/icon.png</icon>
<configuration-action-class>
com.meera.portletconfig.PortletConfigurationAction
</configuration-action-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>
portlet-configuration-action-portlet
</css-class-wrapper>
</portlet>

Download portlet from following location


In download you can see source code and war file you can use war file to deploy directly in your portal server and this portlet was developed using Liferay 6.2 Plugins SDK.

Usage of portlet

Download portlet and deploy the portlet in your portal server

Login as Administrator then add portlet to page which is available in Sample Category with name Portlet Configuration

Go to Portlet configuration page click on portlet configuration icon then you can see portlet configuration page.


Select site name then save the configurations


 Finally selected site users will be displayed in the portlet view page and we can change site name when ever we want like simple go to configuration page select the site name.
 
Author

Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment