30 May, 2012

Add configuration page in plugin portlet

Many times you will get requirement to set portlet configuration.
So if you are not familiar how to add configuration page in liferay plugin portlet, just use below steps with sample code :



1) Create simple plugin portlet (Please check : http://www.liferaysolution.com/2012/05/create-simple-plugin-portlet-liferay.html)

2) Now update liferay-portlet.xml file with configuration-action-class xml tab
<portlet>
        <portlet-name>product-master</portlet-name>
        <icon>/icon.png</icon>
        <configuration-action-class>com.cignex.action.ProductMasterConfigurationActionImpl</configuration-action-class>
        <instanceable>false</instanceable>
        <header-portlet-css>/css/main.css</header-portlet-css>
        <footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
        <css-class-wrapper>product-master-portlet</css-class-wrapper>
    </portlet> 
3)Now define ProductMasterConfigurationActionImpl class with processAction and render method given as below :
package com.cignex.action;

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

import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class ProductMasterConfigurationActionImpl  extends DefaultConfigurationAction{
 public void processAction(PortletConfig config, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { 
    String portletResource = ParamUtil.getString(actionRequest, "portletResource"); 
    javax.portlet.PortletPreferences prefs = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource); 
    
    System.out.println(actionRequest.getParameter("company"));
    prefs.setValue("company", actionRequest.getParameter("company"));
    prefs.store();
   
    // SessionMessages.add(actionRequest, config.getPortletName() + ".doConfigure");
    super.processAction(config, actionRequest, actionResponse);
}

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

  return "/jsp/configuration.jsp";
  }
}

4) Now create JSP(configuration.jsp) file which we have referred in render method with given below code
<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%>

" method="post" name="fm"> Type:
5)You can check prefrence in view.jsp file by putting below code:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<%@page import="javax.portlet.PortletPreferences"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>
<% 
PortletPreferences preferences = renderRequest.getPreferences();

String portletResource = ParamUtil.getString(request, "portletResource");

if (Validator.isNotNull(portletResource)) {
 preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}
%>
You have selected <%= preferences.getValue("company",null) %>
6) Now deploy your portlet and check by clicking on portlet configuration button.
(For portlet deployment, you can check http://www.liferaysolution.com/2012/05/create-simple-plugin-portlet-liferay.html)
Hope this simple information help you to create liferay configuration page.

Rate Me:

Create Servlet Project as Module

You would have seen that many times we are writing out own servlet in Liferay portal to achieve some specific use case. In DXP, You can st...