To create a PortletURL
with Liferay is quite easy with the taglibs provided by the portal; but if we had the need to create some URLs at runtime, based on the value of some field of a form, how can we do that? We should use other libraries, with Javascript.
If we were inside a JSP page, the creation of a URL would be fairly straightforward:
<liferay-portlet:actionURL />
<liferay-portlet:renderURL />
<liferay-portlet:resourceURL />
Using taglibs we create hard coded URL inside the page; however, sometimes you may need to create a URL programmatically but fortunately we can use the Javascript libraries provided by the portal. Here's how:
<aui:script use="liferay-portlet-url"> var portletURL = Liferay.PortletURL.createRenderURL(); portletURL.setPortletId('<%=PortletKeys.SOME_PORTLET %>'); portletURL.setPlid(15932); portletURL.setWindowState('<%=LiferayWindowState.POP_UP.toString() %>'); portletURL.setParameter('param1', param1); portletURL.setParameter('param2', param2); portletURL.setParameter('mvcPath', '/html/my_portlet/my_page.jsp'); // Now we can use the URL console.log(portletURL.toString()); </aui:script>
Similarly you can also create other URL type as needed:
Liferay.PortletURL.createActionURL()
Liferay.PortletURL.createPermissionURL()
Liferay.PortletURL.createResourceURL()
Liferay.PortletURL.createURL()
If you're curious, you can see the module sources: liferay-portal-6.2-ce-ga3\tomcat-7.0.42\webapps\ROOT\html\js\liferay\portlet_url.js
.