In Liferay-portlet.xml we need to specify the configuration action class.

add the below code
<portlet>
		<portlet-name>Portletname</portlet-name>
		<icon>/icon.png</icon>
		<configuration-action-class>
                          com.liferay.config.ConfigurationActionImpl
                </configuration-action-class>
		......
                ......
	</portlet>

create ConfigurationActionImpl.java file in the com.liferay.. package.
aad the below code

package com.liferay.config;

import com.liferay.portal.kernel.portlet.ConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
 * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
 *
 * @author Mark Wong
 *
 */
public class ConfigurationActionImpl implements ConfigurationAction {

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

		String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

		if (!cmd.equals(Constants.UPDATE)) {
			return;
		}

		String email = ParamUtil.getString(actionRequest, "email");
		String subject = ParamUtil.getString(actionRequest, "subject");

		String portletResource = ParamUtil.getString(actionRequest,"portletResource");
		PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);

		preferences.setValue("email", email);
		preferences.setValue("subject", subject);

		preferences.store();

		PortletSession portletSession = actionRequest.getPortletSession();
		SessionMessages.add(actionRequest, portletConfig.getPortletName()+ ".doConfigure");

	}

	public String render(PortletConfig portletConfig,
			RenderRequest renderRequest, RenderResponse renderResponse)
			throws Exception {

		return "/html/config.jsp";
	}

}

Create file Config.jsp in html folder
add the below code

<%@include file="/html/init.jsp" %>

<form action="<liferay-portlet:actionURL portletConfiguration="true" />"
method="post" name="<portlet:namespace />fm">

<input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />

<table class="lfr-table">
<tr>
	<td>Email</td>
		<td><input type="text" name="<portlet:namespace />email"  value="<%=email %>"/>
	</td>

	<td>Subject</td>
		<td><input type="text" name="<portlet:namespace />subject"  value="<%=subject %>" />
	</td>
</tr>
<tr>
       <td colspan="2">
            <input type="button" value="<liferay-ui:message key="save" />"
onClick="submitForm(document.<portlet:namespace />fm);" />
       </td>
</tr>
</table>
</form>

create init.jsp file in htmlfolder add the below code

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

<%@ 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" %>

<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.StringPool" %>
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<%@ page import="com.liferay.portlet.PortletPreferencesFactoryUtil" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="javax.portlet.WindowState" %>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="javax.portlet.ActionRequest" %>
<%@ page import="javax.portlet.PortletPreferences" %>

<%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
<%@ page import="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  email = preferences.getValue("email", StringPool.BLANK);
String  subject = preferences.getValue("subject", StringPool.BLANK);

%>

Craete view.jsp file to display the configuration value use the below code

<%@include file="/html/init.jsp" %>
<%=email %>
<%=subject %>
Advertisements

Comments on: "Configuration the portlet in liferay using portletPreference" (10)

  1. HI
    Thanks for your excellent posting. I was looking for this one. I am very much glad to you.

    Regards
    Rony.

  2. chilokan said:

    Hello,

    is needed in the init.jsp

    • Hi Chilokan,

      To avoid the redundancy of the code i am using init.jsp file,
      its not mandatory, but for good practice i am suggesting go for init.jsp file.

      Thanks and Regards,
      Rajeeva Lochana.B.R

  3. Nisha solanki said:

    Thank for the post..
    I have doubt can u please tell y do we actually use preference portlet??

  4. Sasmita Swain said:

    Thanks for the post..when i am deploying this portlet error is comming like com.test.ConfigurationActionImpl cannot be cast to javax.portlet.Portlet

  5. It is nice post!

    But i want to change name of the tab Like “Setup” to “Config”.

    Can you please guide me ?

  6. syed fahad said:

    thanks for this post. It was very helpful. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: