0
votes

I am stuck with a probably simple Spring Portlet redirect and jsp render problem in Liferay.

I have two Pages in Liferay page1 and page2 and one spring mvc portlet portletA with two JSPs defaultView.jsp and second.jsp.

page1 and page2 both instantiate portletA.

portletA loads by default the defaultView.jsp by calling the @RenderMapping() method.

DefaultViewController:

@RenderMapping
public String renderDefaultView() {
    return "defaultView";
}

From defaultView.jsp the liferay page2 can be called by http redirect (@ActionMapping resp.sendRedirect())

defaultView.jsp:

 $(nRow).click(function() {
      window.location.href = "<portlet:actionURL name='doGotoPage2' />"
 });

DefaultViewController:

@ActionMapping("doGotoPage2")
public void doGoToMedicalAid(ActionRequest req,
        ActionResponse resp) {

    try {
        resp.sendRedirect("/group/project/page2");
    } catch (IOException ex) {
        Logger.getLogger(DefaultViewController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Now I am wondering

how can I tell portletA to call a different @RenderMapping method on page2 after the redirect from page1 ?

I would like portletA to render second.jsp on page2:

@RenderMapping(value = "renderSecondPage")
public String renderSecondPage() {
    return "second";
}

As far as I understand, I can't set renderParameters in the action phase before or after a sendRedirect!

For example, this doesn't work and results in an java.lang.IllegalStateException: Set render parameter has already been called

 resp.sendRedirect("/group/project/page2");
 resp.setRenderParameter("render", "renderSecondPage");

Thanks a lot for your help!

3

3 Answers

1
votes

The Portlet API docs state that:

The sendRedirect method can not be invoked after any of the following methods of the ActionResponse interface has been called:

  • setPortletMode
  • setWindowState
  • setRenderParameter
  • setRenderParameters

You'll need to use another scope for storing the attribute.

1
votes

http://www.opensource-techblog.com/2012/12/multiple-render-method-in-liferay-mvc.html

Do have a look on the above link. Here on the basis of action method different jsp is being rendered.

1
votes

You need to prepare url of portlet on second page. In liferay its possible by creating Render URL of portlet placed on other liferay page.

PortletURL renderUrl =  PortletURLFactoryUtil.create(request, <<Portlet Id on aonther page>>, <<LayoutId of another page>>,PortletRequest.RENDER_PHASE);
renderUrl.setWindowState(LiferayWindowState.NORMAL);
renderUrl.setPortletMode(LiferayPortletMode.VIEW);
renderUrl.setParameter("action", "<<name of action attribute set for render method>>");

Explanation:-

  • PortletURLFactoryUtil is used to create portlet URL. we have to pass request (portletRequest), portletId which is placed on another page, another page's layout Id and phase(currently render since we want to call its render method).

In spring MVC portlet, you can have multiple render method. The default render method won't have any action attribute while all other have.