resin Action Tutorial tutorial

An action request is a request for the portlet to do something before it is rendered. An action request to a portlet invokes the processAction method before invoking the render method.

This tutorial adds an HTML form to the hello world portlet. The HTML form allows the user to set the "identity" and "color". The submission of the form is an action, it is a request to do something.

Portlet web-app configuration

An action URL is created with response.createActionURL(). The resulting url indicates to the portal that it should invoke the processAction() method in the portlet before invoking the render() method.

A form that uses an action parameter must always use the POST method. The portal may encode some special parameters in the url, and the use of the GET method would overwrite those parameters.

... PortletURL submitUrl = response.createActionURL(); out.println("<form method='POST' action='" + submitUrl + "'>"); out.println("Name:"); out.println("<input type='text' name='identity' value='" + identity + "'>"); out.println("<br>"); out.println("Color:"); out.println("<input type='text' name='color' value='" + color + "'>"); out.println("<br>"); out.println("<input type='submit'"); out.println("</form>"); ... <form method='POST' action='/resin-doc/portlet/tutorial/basic-lifecycle/hello?____A='> Name: <input type='text' name='identity' value='World'> <br> Color: <input type='text' name='color' value='silver'> <br> <input type='submit' </form>

When the form submit button is pressed, the action url is used. The action url indicates to the portal that it is an action request. Responding to the action request, the portal invokes the processAction() method before invoking the render() method.

The resulting HTML demonstrates how the portal might add parameters to the url. The url that is in the action attribute contains the ____A= parameter. The parameter has been added to mark the requet as an action request.

The portlet's implentation of processAction() uses response.getParameter() to get the submitted form parameters.

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { // get the values submitted with the form String identity = request.getParameter("identity"); String color = request.getParameter("color"); ...

There is an important distinction here - during processAction() the parameters are the parameters submitted from a form, during render() the parameters are render parameters and the parameters submitted from a form are no longer available.

If the portlet is invoked from an action url, all render parameters are reset. The implementation of the processAction must use response.setRenderParameter() to set render parameters for the susbsequent call to render().

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { ... response.setRenderParameter("identity", identity); response.setRenderParameter("color", color); }