Foros de discusión

Trouble accessing request parameters

thumbnail
Raymond Gardner, modificado hace 12 años.

Trouble accessing request parameters

Regular Member Mensajes: 118 Fecha de incorporación: 15/07/11 Mensajes recientes
Hello all,

I have a situation where I am on page 1, portlet A. I click on a link in portlet A.
This invokes an action handler in portlet A's controller.

In this handler I want to redirect the request to portlet B on page 2 and I also want to
pass along some request parameters for portlet B to process.

Here is the code I'm using in portlet A's handler to create the link and redirect it:

PortletURL bPortletUrl = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(request),
                                                                                                 "portletB",
                                                                                                 PortalUtil.getPlidFromFriendlyURL(td.getCompanyId(), page2.getFriendlyURL()),
                                                                                                 PortletRequest.RENDER_PHASE);
bPortletUrl.setParameter("arg1", request.getParameter("arg1FromLinkClickedByUser"));
response.sendRedirect(bPortletUrl.toString());


Here is the code in portlet B's render handler:

String arg1Value = renderRequest.getParameter("arg1");


arg1Value is null every time. The strange thing is, I see a "_portletB_arg1" on the url in the browser. But,
I can't seem to access it by this parameter name either. Liferay is automatically prefixing "_portletB_".

Should I use an ACTION_PHASE instead of a RENDER_PHASE?
thumbnail
Raymond Gardner, modificado hace 12 años.

RE: Trouble accessing request parameters

Regular Member Mensajes: 118 Fecha de incorporación: 15/07/11 Mensajes recientes
Fantastic!

I figured out how to do this. Let me share it with everybody.

Inter-Portlet Communication using the PortletSession.
There are other ways to accomplish Inter-Portlet Communication, such as with events.
But, why bother when working with something simple, quick and small!

In the sending or source portlet's action phase method, put the attributes, or arguments, on the PortletSession
with a scope of Application. Then, create a url to the new porltet/page and redirect:

actionRequest.getPortletSession().setAttribute("arg1", actionRequest.getParameter("param1FromLinkClickedByUser"),
                                                                                     PortletSession.APPLICATION_SCOPE);
PortletURL portletBPortletUrl = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(request),
                                                                                                             "portletB",
                                                                                                             PortalUtil.getPlidFromFriendlyURL(td.getCompanyId(),
                                                                    		                                                                  page2.getFriendlyURL()),
                                                                                                              PortletRequest.RENDER_PHASE);
portletBPortletUrl.setWindowState(WindowState.NORMAL);
portletBPortletUrl.setPortletMode(PortletMode.VIEW);
actionResponse.sendRedirect(portletBPortletUrl.toString());


In the receiving or target portlet's render phase method, get the attributes, or arguments as needed
so the receiving portlet may make use of it:

String arg1 = (String)renderRequest.getPortletSession().getAttribute("arg1", PortletSession.APPLICATION_SCOPE);
thumbnail
Raymond Gardner, modificado hace 12 años.

RE: Trouble accessing request parameters

Regular Member Mensajes: 118 Fecha de incorporación: 15/07/11 Mensajes recientes
One more thing to note, once the attribute is consumed it needs to be removed from the portlet session.
Otherwise, your code is likely to process this attribute in future requests that really are not expecting it to be applied.

Actually, these attributes/arguments really need to be in request scope.