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:

1 Discussion:

Enrico said...

Hi,
i have this problem:
com.liferay.portal.kernel.exception.SystemException: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at com.liferay.portal.service.persistence.PortletPreferencesFinderImpl.findByPortletId(PortletPreferencesFinderImpl.java:59)
at com.test.Fer_Kin_Slice.doPost(Fer_Kin_Slice.java:174)
at com.test.Fer_Kin_Slice.doGet(Fer_Kin_Slice.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:698)
at com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl.openSession(SessionFactoryImpl.java:59)
at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.openSession(BasePersistenceImpl.java:177)
at com.liferay.portal.service.persistence.PortletPreferencesFinderImpl.findByPortletId(PortletPreferencesFinderImpl.java:44)
... 16 more

when i call the method getPortletSetup:

PortletPreferences preferences1=(PortletPreferences) PortletPreferencesFactoryUtil.getPortletSetup(LayoutLocalServiceUtil.getLayout(plid), prefercences.get(k).getPortletId(), defaultpref);
String stringapref = ((javax.portlet.PortletPreferences) preferences1).getValue("identifier", "");
((javax.portlet.PortletPreferences) preferences1).store();
I use an external servlet, so i can't use the HttpServletRequest. Can it use a dinmicquery to find the PortletSetup?
Can you help me?

Thank you.