PDA

View Full Version : Spring Portlet MVC - cross portlet communication


Eug9n9
Feb 10th, 2006, 09:13 AM
Hello all,

I am trying to develop a simple test: 2 spring-portlet-mvc-based portlets should "talk" to each other - via an attribute, stored in the PortletSession.

According to this Inter-portlet communication (https://www.dev.java.net/files/documents/1654/8898/tip1.html) tip, using the portlet api I should:

1) In processAction() of the 1st portlet store the attribute:
portletSession.setAttribute("cityName", newCityName, PortletSession.APPLICATION_SCOPE);

2) In doView() of the 2nd portlet get the attribute:
currentCityName=(String)portletSession.getAttribut e("cityName",PortletSession.APPLICATION_SCOPE);

Now, How do I do it with Spring Portlet MVC?

p.s. AFAIK PortletUtils.set/getSessionAttribute() provides this functionality, but what are the right places to put these methods?

dsklyut
Feb 13th, 2006, 11:21 PM
You do it the same way :)

in the controller mapped to portlet A in handleActionRequestInternal (or corresponding sub template method) add a value to the PortletSession

in the controller mapped to portlet B in handleRenderRequestInternal (or corresponding sub template method) get a value from the PortletSession.

You could also use events in the ApplicationContext to get similar results.

Eug9n9
Feb 14th, 2006, 01:59 AM
You do it the same way :)
Thank you very much for your reply!

I've managed to get it working with spring portlet MVC. It is even possible to set the session var in handleRenderRequestInternal() of the controller of the 1st portlet. At least it works with the consecutive portlet layout.

Here is the code of the "sender" portlet controller:

public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {

PortletSession portletSession = request.getPortletSession(true);

// get the id
Long id = new Long(request.getParameter("id"));

// and send it
String sId = id.toString();
portletSession.setAttribute("sId", sId, PortletSession.APPLICATION_SCOPE);
...}

And the "receiver" one:

public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {

PortletSession portletSession = request.getPortletSession(true);

// receive the id
String sId = (String) portletSession.getAttribute("sId", PortletSession.APPLICATION_SCOPE);
...}

dsklyut
Feb 14th, 2006, 09:26 AM
You can't relly on the order that container will invoke the render for the portlet. Just be carefull, your receiver can be invoked before the sender.

Dmitry

Eug9n9
Feb 14th, 2006, 03:10 PM
Yes, you are right. Thank you again.