4

In the method processAction(ActionRequest request, ActionResponse response), I insert a record into database and get the ID and then I want to redirect to the view page of this record. So I need to create a RenderURL with a parameter value for that ID.

ActionResponse doesn't provide method to create a renderURL. Some codes in Liferay do similar things like:

  1. create renderURL before accessing the actionURL
  2. pass the renderURL as a parameter in the actionURL

However, at that time, I don't know the value of ID.

Other codes also use new PortletURLImpl() directly. My portlet cannot see that class.

Prakash K
  • 11,669
  • 6
  • 51
  • 109
bopomofu
  • 143
  • 1
  • 1
  • 7
  • Sorry, I didn't say it clearly. I want the `renderURL` as an redirect URL. If you don't redirect, when you refresh or press enter in the address bar, you'll get wrong page result. – bopomofu Nov 02 '12 at 09:44

4 Answers4

2

Other codes also use new PortletURLImpl() directly. My portlet cannot see that class.

Because this class is in portal-impl.jar and also it is not recommended to use classes from this jar. Starting from Liferay 6.1, you won't be able to build your portlet from plugins-sdk if you classes point to portal-impl.jar.

Now to answer your question:

Any jsp is rendered by the render method or doView method (if using liferay's MVCPortlet) and this method would be called as part of the normal life-cycle of portlets.

Here are the steps you would need to take:

  1. set a render parameter (using response.setRenderParameter() method) in your `processAction' method at the last which would be available in your render method, as follows:

    actionResponse.setRenderParameter("myID", 1201);
    

    Just for info: After using setRenderParameter you cannot use sendRedirect method

  2. fetch this "myID" in your render method as you fetch any other request parameter:

    //assuming your ID is a long
    long myUserName = ParamUtil.getLong(renderRequest, "myID");
    

    or

    String strMyID = renderRequest.getParameter("myID");
    long myID = Long.parseLong(strMyID);
    
  3. After this, just use

    include(renderPage, renderRequest, renderResponse);
    

    were renderPage is nothing but a string containing the path to your jsp within docroot like /html/yourportlet/view.jsp

    Just as an afterthought:
    If you are using a Liferay IDE, then you can try creating a simple portlet project with MVCPortlet and then look at the generated portlet.xml's <init-param>

So basically you need to pass information from action-phase to render-phase, the development guide is a good place for explaining this in detail.

That's it.
Hope this helps.

Let me know if you have any confusion regarding this.

Prakash K
  • 11,669
  • 6
  • 51
  • 109
2

In action phase do the following:

ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute (WebKeys.THEME_DISPLAY);
PortletURL url = PortletURLFactoryUtil.create(request, this.getPortletName(), themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);

For example, if you want to redirect to the login page and back, you can do the following:

response.sendRedirect("/c/portal/login?redirect=" + HttpUtil.encodeURL(url.toString()));

Definitely you can add or copy the parameters as required.

jenzz
  • 7,239
  • 6
  • 49
  • 69
0

Instead of creating the renderURL you can include the view page include(viewTemplate,actionRequest,actionResponse). Or if you want to sent any parameter any want's to get it in doView then use actionResponse.setParameter(name,value) method

lucky
  • 147
  • 5
-1

I create a RenderURL with a place holder as parameter value, like this:

   <portlet:renderURL var="redirect">
      <portlet:param name="ID" value="__ID__" />
   </portlet:renderURL>`

In processAction:

    String redirect = redirectParam.replace("__ID__", "123213");
    actionResponse.sendRedirect(redirect) ;
bopomofu
  • 143
  • 1
  • 1
  • 7