How to create a renderderl during an action phase in a portlet?

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

ActionResponse does not provide a method for creating a RenderURL . Some codes in Liferay do similar things, such as:

  • create a RenderURL before accessing actionURL
  • pass RenderURL as parameter in actionURL

However, at that time I do not know the ID value.

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

+4
source share
4 answers

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

Since this class is located in portal-impl.jar , it is also not recommended to use classes from this jar. Starting with Liferay 6.1, you cannot build your portlet from plugins-sdk if the classes point to portal-impl.jar .

Now, to answer your question:

Any jsp is rendered using the render or doView method (if using liferay MVCPortlet ), and this method will be called part of the normal portlet life cycle.

Here are the steps you need to take:

  • set the render parameter (using response.setRenderParameter () in your processAction method, which will be available in your rendering method, as follows:

     actionResponse.setRenderParameter("myID", 1201); 

    For information only: After using setRenderParameter you cannot use sendRedirect

  • extract this "myID" in your rendering method when you receive 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); 
  • After that just use

     include(renderPage, renderRequest, renderResponse); 

    were renderPage - this is nothing more than a string containing the path to your jsp inside the docroot , e.g. /html/yourportlet/view.jsp

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

Thus, basically you need to transfer information from the action phase to visualization, the development guide is a good place to explain this in detail.

What is it.
Hope this helps.

Let me know if you have any confusion in this regard.

+2
source

In the 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())); 

You can definitely add or copy parameters as needed.

+2
source

Instead of creating renderURL, you can include a view page (viewTemplate, actionRequest, actionResponse). Or, if you want to send any parameter that you want to get it to doView, use the actionResponse.setParameter (name, value) method

0
source

I create a RenderURL with a place holder as a parameter value, for example:

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

In processAction :

  String redirect = redirectParam.replace("__ID__", "123213"); actionResponse.sendRedirect(redirect) ; 
-1
source

Source: https://habr.com/ru/post/1443353/


All Articles