Passing data between action and render phase – Liferay

Sometimes we want to pass parameters from the action phase to the render phase(jsp page). In this article, we will implement passing the data between the action and render phase.

We can achieve this in two ways in Liferay CMS.

Passing data between action and render phase

Let us see different ways of implementing this.

First method

The action parameters set for the action URL will be available in the action phase. These parameters can be sent to the render phase or JSP page by the following method.

actionResponse.setRenderParameter("parameter-name", "parameter-value");

In this approach, we forward each action parameter using the actionResponse.setRenderParameter() method.

In the below code snippet, we are setting “parameter-name” as “mvcPath” and “parameter-value” to the jsp path.

With the Liferay MVC Portlet implementation, we can make all these parameters available to the render phase by setting the following attribute in the Portlet.xml file.

<init-param>
 <name>copy-request-parameters</name>
 <value>true</value>
</init-param>

These parameters will be available even if the page gets refreshed because of other portlet actions.

Second method

The second way of passing data between the action and render phase is by using the session object.

Liferay provides a helper class called “SessionMessages” and tag libraries to show the messages on the jsp page.

We can use SessionMessages to add the messages as shown below:

 SessionMessages.add(actionRequest, "success");

To access these values from the jsp page, we can use the <portlet:definedObjects/> tag library as shown below:

<portlet:defineObjects />

<liferay-ui:success key="success" message="successful!"/>

Also, for error messages, we can use SessionErrors class and tag as shown below.

SessionErrors.add(actionRequest, "error");

//To retrieve in JSP:
<liferay-ui:error key="error" message="Sorry, Error occurred!!" />

Conclusion

In this article, we learned how to pass information from Liferay’s action phase to the render phase.