简体   繁体   中英

Liferay portlet: redirect to an other jsp page from javascript

I want to redirect to an other jsp page in a liferay portlet from js. The idea that I found is using a renderurl. The best code that I found that can help me as a first step is this one:

<a id="renderURLWithJS" href=""> This render URL link is created with Javascript</a>
<aui:script>
    AUI().use('liferay-portlet-url', function(A) {
    var param="Hello new jsp"; 
    var renderUrl1 = Liferay.PortletURL.createRenderURL();
    renderUrl1.setWindowState("<%=LiferayWindowState.NORMAL.toString() %>");
    renderUrl1.setParameter("param",param);
    renderUrl1.setParameter('mvcPath', 'display.jsp'); /* is this correct???*/
    renderUrl1.setPortletMode("<%=LiferayPortletMode.VIEW %>");
    A.one("#renderURLWithJS").set('href',renderUrl1.toString());
    });
</aui:script>

But the problem is that I'm always redirected to the same actual page (view.jsp) not to the jsp page that I want display.jsp.

Does someone have an idea what's wrong with my code?

Liferay needs to know the target portlet for any request parameter that you add to an URL - otherwise it ignores any unknown parameter. The ID of the target portlet is either defined as p_p_id or it is a prefix of the parameter (in which case you can define parameters for more than one portlet in one URL).

So in your case you will have to set renderUrl1.setPortletId('...your portlet id'); to let your portlet see mvcPath and param .

In addition the mvcPath must be absolute (start with a / ) and below the configured templatePath (defaults to / ).

Try this:

In your JSP ->

<portlet:renderURL var="myRenderURL">
  <portlet:param name="jspName" value="display.jsp" />
</portlet:renderURL>

<aui:a href="#" cssClass="myLink" label="Click me" />

<aui:script>
  A.one('.myLink').on('click', function () {
    window.location.href = '${myRenderURL}';
  });
</aui:script>

Then in your java portlet class ->

@Override
public void doView(RenderRequest request, RenderResponse response) 
    throws IOException, PortletException {

  String jspName = ParamUtil.getString("jspName", "view.jsp");
  include("path/to/your/jsp/" + jspName, request, response);

}

One last note: Starting with Liferay 6.2, portlet urls created with javascript methods mentioned in the OP above will

  • not include authentication tokens (p_auth, p_p_auth)
  • not be converted to friendly urls by the portal.

Also see the Liferay Documentation on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM