Configuration Page Example

We'll need to provide a class that extends BaseConfigurationAction and Override render() and processAction(..) methods for handling configuration.

In ProductMasterConfigurationActionImpl.java:

   package com.cpage.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.BaseConfigurationAction;
   import com.liferay.portal.kernel.util.ParamUtil;
   import com.liferay.portlet.PortletPreferencesFactoryUtil;
       
public class ProductMasterConfigurationActionImpl extends    BaseConfigurationAction {
   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();
super.processAction(config, actionRequest, actionResponse);
      
}    
  public String render(PortletConfig config, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception 
{
        return "/html/cpage/configuration.jsp";
 }
}

In configuration.jsp:

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<portlet:defineObjects />
<form action="<liferay-portlet:actionURL portletConfiguration="true" />" method="post" name="<portlet:namespace />fm">
<input name="<portlet:namespace /><%=Constants.CMD%>" type="hidden" value="<%=Constants.UPDATE%>" />
Type: <select name="<portlet:namespace />company">
<option value="casual">Casual</option>
<option value="formal">Formal</option>
</select> <br/>
<input type="button" value="Save" onClick="submitForm(document.<portlet:namespace />fm);" />
</form>

In liferay-portlet.xml
 Make entry in liferay-portlet.xml add the <configuration-action-class> tag.

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.0.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_0_0.dtd">

<liferay-portlet-app>
      <portlet>
            <portlet-name>cpage</portlet-name>
            <icon>/icon.png</icon>
<configuration-action-class>
com.cpage.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>cpage-portlet</css-class-wrapper>
      </portlet>
      <role-mapper>
            <role-name>administrator</role-name>
            <role-link>Administrator</role-link>
      </role-mapper>
      <role-mapper>
            <role-name>guest</role-name>
            <role-link>Guest</role-link>
      </role-mapper>
      <role-mapper>
            <role-name>power-user</role-name>
            <role-link>Power User</role-link>
      </role-mapper>
      <role-mapper>
            <role-name>user</role-name>
            <role-link>User</role-link>
      </role-mapper>
</liferay-portlet-app>

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.