Question

I am trying to redirect to other page using java code and don't no why its not getting redirected. the below is the following code

ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);


PortletConfig portletConfig = (PortletConfig) actionRequest.getAttribute("javax.portlet.config");

String portletName = portletConfig.getPortletName();

PortletURL successPageURL = PortletURLFactoryUtil.create(
                actionRequest, portletName+ "_WAR_" + portletName + "portlet", 
                themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);

successPageURL.setParameter("jspPage", LibraryConstants.PAGE_SUCCESS);
actionResponse.sendRedirect(successPageURL.toString());

it's not redirecting to the page. Please help to check if the code is correct or not.

Thanks.

Was it helpful?

Solution 2

I assume this code is in an action method or processAction method.

Instead of re-directing from action-phase directly it would be better to use actionResponse.sendRenderParameter("jspPage", LibraryConstants.PAGE_SUCCESS) to set the jspPage to be rendered through the render or doView method.

Also it would be helpful to check the sendRedirect documentation, where it is mentioned that it won't redirect in certain cases.

Also please check if you get any error or warning when the code is executed.

OTHER TIPS

the problem it didn't redirect because we have to add a tag in liferay-portlet.xml that tag is

      <action-url-redirect>true</action-url-redirect>

after above line it worked.

With that code, you're not redirecting to another page, you're only giving another portlet's id - there's no clue which page you'd like to be forwarding to.

I don't have an environment for a quick check available during the weekend (sorry), here's what you can try to do to find the solution yourself:

  • Inspect the URL you're building - you'll see that it addresses the current page that you're on.
  • Find a way to actually provide the name of the page you want to redirect to. Hint: The portlet you want to redirect to could be placed on many pages, or on none. You'll have to either dynamically find one of the pages that it's on (and hope it's the one you actually want) or configure it somehow.
  • Also, architecturally I find it's a lot better to "just" link to a simple page, rather than to a portlet's URL on a target page. Otherwise you're coupling two distinct portlets quite closely together - changes to one might need to be reflected in the other. Rather use parameters (e.g. public render parameters) to communicate between different portlets - instead of addressing another portlet more or less directly.

I hope that this helps you find the solution yourself as I don't have an IDE available on this computer to provide sample code.

You need to find the URL of the page first. You could get it from the friendly URL (friendlyURL):

  1. ThemeDisplay theme = (ThemeDisplay) getPortletRequest().getAttribute(WebKeys.THEME_DISPLAY);

  2. final long GROUP_ID = theme.getLayout().getGroupId(); Layout destinationLayout = LayoutLocalServiceUtil.getFriendlyURLLayout(GROUP_ID, false, friendlyUrl);

  3. Layout layout = LayoutLocalServiceUtil.getLayout(destinationLayout.getPlid()); String completeUrl = PortalUtil.getLayoutFullURL(layout, getThemeDisplay());

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top