Search code examples
javaliferayportlet

No edit mode in Liferay portlet


I'm following along with the Liferay In Action book. I'm at the part where I am adding edit-mode to a portlet. The portlet deployed successfully and I've added the portlet and now the book says to click the wrench in the portlet and click the Preferences link but I don't have a Preferences link. View is working fine.

Here is my portlet.xml:

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
<portlet>
    <portlet-name>hello-john</portlet-name>
    <display-name>Hello John</display-name>
    <portlet-class>com.liferaytest.portlet.HelloJohnPortlet</portlet-class>
    <init-param>
        <name>view-jsp</name>
        <value>/view.jsp</value>
    </init-param>
    <init-param>
        <name>edit-jsp</name>
        <value>/edit.jsp</value>
    </init-param>
    <expiration-cache>0</expiration-cache>
    <supports>
        <mime-type>text/html</mime-type>
        <portlet-mode>view</portlet-mode>
        <portlet-mode>edit</portlet-mode>
    </supports>
    <portlet-info>
        <title>Hello John</title>
        <short-title>Hello John</short-title>
        <keywords>Hello John</keywords>
    </portlet-info>
    <security-role-ref>
        <role-name>administrator</role-name>
    </security-role-ref>
    <security-role-ref>
        <role-name>guest</role-name>
    </security-role-ref>
    <security-role-ref>
        <role-name>power-user</role-name>
    </security-role-ref>
    <security-role-ref>
        <role-name>user</role-name>
    </security-role-ref>
</portlet>

My edit.jsp:

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

<jsp:useBean class="java.lang.String" id="addNameURL" scope="request" />

<portlet:defineObjects />

<form 
id ="<portlet:namespace />helloForm" 
action="<%= addNameURL %>" 
method="post">
<table>
    <tr>
        <td>Name:</td>
        <td><input type="text" name ="username"></td>
    </tr>
</table>
<input type="submit" id="nameButton" title="Add Name" value="Add Name">
</form>

My doEdit method:

public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse)
    throws IOException, PortletException {
    renderResponse.setContentType("text/html");
    PortletURL addNameURL = renderResponse.createActionURL();
    addNameURL.setParameter("addName", "addName");
    renderRequest.setAttribute("addNameURL", addNameURL.toString());
    include(editJSP, renderRequest, renderResponse);
}

Solution

  • To have preferences(configuration) page in your portlet in Liferay you must implement com.liferay.portal.kernel.portlet.ConfigurationAction interface and configure portlet in liferay-portlet.xml to use you class.

    <portlet>
      <portlet-name>MyPortlet</portlet-name>
      <configuration-action-class>com.mydomain.myportlet.ClassThatImplementsConfigurationAction</configuration-action-class>
      <instanceable>false</instanceable>
      ...
    </portlet>
    

    You should also be aware that inside that class you are in Liferay's configuration portlet, not your portlet. So getting preferences like

    portletRequest.getPreferences();
    

    results in preferences of Liferay-s configuration portlet.

    To get preferences of your portlet add this method to your class

    protected PortletPreferences getPortletPreferences(final PortletRequest p_portletRequest) throws Exception {
        String portletResource = ParamUtil.getString(p_portletRequest, "portletResource");
        PortletPreferences prefs = PortletPreferencesFactoryUtil.getPortletSetup(p_portletRequest, portletResource);
        return prefs;
    }
    

    and call it from implemented methods

    public void processAction(PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse) throws Exception;
    
    public String render(PortletConfig portletConfig, RenderRequest renderRequest,
            RenderResponse renderResponse) throws Exception;