I was checking some Portlet tutorial when I came across the following concept :
Due to the separated action and rendering model, the action request attributes will not be available in any of the view components that are included in the render phase (JSP, Servlet etc)
and the mentioned code was as follows :
public class DateTimePortlet extends GenericPortlet {
public void doView(RenderRequest req, RenderResponse res) throws IOException, PortletException {
Object actionAttribute = req.getAttribute("datetime");
res.getWriter().println("Date Time:" + (actionAttribute != null ? actionAttribute :"Unavailable"));
res.getWriter().close();
}
public void processAction(ActionRequest req, ActionResponse res) throws PortletException {
req.setAttribute("datetime",new Date());
}
}
And actually I don't understand why is this happening .... isn't the render method always called after the action method? .... so they run in the same scope?
Request's attributes concerns only to request itself. If you want to pass information from the Action Phase to Render Phase, you can call many times this method:
actionResponse.setRenderParameter("parameter-key","value");
and after in the RenderPhase you can get the information:
renderRequest.getParameter("parameter-key");
There are other ways to do this like using the request session or using the Liferay Portlet Preferences. For example:
actionRequest.getPortletSession().setAttribute("session-key",value);
actionRequest.getPreferences().setValue("preferences-key","value");
and after:
renderRequest.getPortletSession().getAttribute("session-key");
renderRequest.getPreferences().getValue("preferences-key","default-value");