简体   繁体   中英

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

  • set the parameter to a shared/application session

  • read it from the session and pass it to the view using Spring Model

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM