Dropout
Dropout

Reputation: 13866

Passing an attribute from one portlet to another via session

I am using Spring-MVC and Liferay and I need to pass an attribute from one portlet to another through session.

Do I need to use HttpSession instead of PortletSession, or is the APPLICATION_SCOPE setting enough?

I need to do two things

I am trying to do the first like this:

PortletSession session = request.getPortletSession();
session.setAttribute("foo", request.getParameter("foo"), 
    PortletSession.APPLICATION_SCOPE);
response.sendRedirect("/somewhere");

And then the second like this:

@RequestMapping
public String view(PortletSession session, Model model){
    if (session.getAttribute("foo") != null) {
        model.addAttribute("foo", session.getAttribute("foo").toString());
    }
return "somewhere/view";
}

And then I try to display it in my JSP simply by using ${foo} but nothing shows up.

Can you share any advice please?

Upvotes: 0

Views: 1597

Answers (1)

Dropout
Dropout

Reputation: 13866

I tried to get the attribute from the session in a bad way. I need to specify the scope while retrieving the attribute from PortletSession as well.

Changing it to

@RequestMapping
public String view(RenderRequest request, Model model){
    PortletSession session = request.getPortletSession();
    if (session.getAttribute("foo", 
            PortletSession.APPLICATION_SCOPE) != null) {
        model.addAttribute("foo", session.getAttribute("foo", 
            PortletSession.APPLICATION_SCOPE).toString());
    }
    return "somewhere/view";
}

fixed the problem.

Also it is necessary to set the private session attributes setting to false in liferay-portlet.xml in the module of both portlets like this:

<portlet>
    <!-- ..some previous settings and then -->
    <private-session-attributes>false</private-session-attributes>
</portlet>

Doc: http://www.liferay.com/community/wiki/-/wiki/Main/Portlet+to+Portlet+Communication

Upvotes: 1

Related Questions