Action Phase Example

In Action Phase, processAction(…) method is invoked.

package com.test;
 
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
 
/**
 * Portlet implementation class ActionPortlet
 */
public class ActionPortlet extends GenericPortlet {
 
    public void init() {
        viewJSP = getInitParameter("view-jsp");
    }
 
    public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException, PortletException {
 
        // Pass information from Action Phase to Render Phase
        actionResponse.setRenderParameter("state","Actioned.");
    }
 
    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {
 
        String state = renderRequest.getParameter("state");
        renderRequest.setAttribute("state", state);
        include(viewJSP, renderRequest, renderResponse);
    }
 
    protected void include(
            String path, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws IOException, PortletException {
 
        PortletRequestDispatcher portletRequestDispatcher =
            getPortletContext().getRequestDispatcher(path);
 
        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        }
        else {
            portletRequestDispatcher.include(renderRequest, renderResponse);
        }
    }
 
    protected String viewJSP;
 
    private static Log _log = LogFactoryUtil.getLog(ActionPortlet.class);
 
}

View:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
 
<portlet:defineObjects />
 
<portlet:actionURL var="testAction" />
 
<aui:form action="<%= testAction %>" method="post">
    <aui:input label="Input" name="greeting" type="text" />
    <aui:button type="submit" />
</aui:form>
 
State: ${state}
process-action.png
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License