Monday, November 11, 2013

Working with Liferay URLs

Working with Liferay URLs


In liferay development we have many options to create liferay URLs i.e.
Render URL, Action URL and Resource URL.
We have many ways to create liferay URL in development the following are the ways to create URLs

  1. Using tag library
  2. Using complete java classes and java objects
  3. Using Java script


Using Tag Library:

Liferay provide build in tag library from that we can create liferay URLs.
We have two tag libraries from which we can create URLs

  • Portlet tag library
  • Liferay Portlet tag library.


Portlet Tag Library :

Portlet tag library is stand tag library for every portal means it’s just JSR 168 and JSR 286 standard tag library.

To make use of this tag library in jsp page we need to use following tag.


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>


Render URL creation:



<portlet:renderURL var="simpleDialogIframeExample" windowState="<%=LiferayWindowState.POP_UP.toString()%>">
<portlet:param name="mvcPath" value="/html/alloyuidialog/iframe_alloyui_dialog_example.jsp"/>
</portlet:renderURL>


The following are important attribute for tag:

var : This is variable name so that we can reference URL by this name
Window state: This is liferay window state like normal. Maximized ,minimized and pop_up.
portlet mode : this is mode of portlet view or edit
copyCurrentRenderParameters: This is whether you want copy all render parameters
If you want add more addition parameters we should use another tag called <portlet:param/>
This is used to add more parameters or custom parameters

Finally this tag converted and the URL like following

For example my portlet Name:  LiferayAlloyUIDialogportlet
I am creating Render URL in view.jsp page for this portlet
Then finally URL look like this in browser address bar its simple query string




p_p_id: current portlet id
p_p_state: window sate
p_p_mode: portlet mode either view/edit
p_p_lifecycle: this is life cycle of portlet  0/1/2
0: render phase or render URL
1: action phase or action URL
2: server resource URL
mvcPath: this is another parameter which we passed from <portlet:param/>  tag

Note:

Similarly all URL have same parameters like above when we create URL from portlet tag library

Examples for URLs created from portlet tag library:

Render URL:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:renderURL var="simpleDialog" windowState="normal">
<portlet:param name="mvcPath" value="/html/alloyuidialog/simple_alloyui_dialog.jsp"/>
</portlet:renderURL>

Action URL:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:actionURL var="iFrmedilogActionURL" windowState="<%=LiferayWindowState.POP_UP.toString()%>">
<portlet:param name="<%=ActionRequest.ACTION_NAME%>" value="getActionMessageForSimpleDialog"/>
</portlet:actionURL>

Resource URL:

<portlet:resourceURL var="feedBackResourceURL" id="view.jsp" escapeXml="false" />

Note:

With this tag library we can create URL for only current portlet .means where jsp page is located in portlet for that particular portlet only we can create.

Liferay Portlet Tag library:

This is another tag library which is from liferay by this we can create URLs but the difference between standard tab library and liferay tag library is  we can create URL for other portlet also.
Means if you work with PortletA in portletA JSP page we can create URL for PortletB. This is using in some scenarios where you want pass some parameters form portletA to portletB.
To make use of this library we need to add following tag library in jsp page


<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>


Example for create Liferay URLs from Liferay Portlet Tag Library:

For example I want create RenderURL for portlet 47 means this is document library portlet

The following is example

<liferay-portlet:renderURL portletName="47" var="openPortletURL" plid="<%=themeDisplay.getPlid()%>"  varImpl="openPortletURL" windowState="<%=LiferayWindowState.POP_UP.toString()%>">
</liferay-portlet:renderURL>

Important Attributes:

portletName: the name of portlet  for which you going to  create URL.
plid :  In which page your portllet is reside. Means its layout id
var : This is variable name so that we can reference URL by this name
Window state: This is liferay window state like normal. maximized, minimized and pop_up.
Portlet Mode: this is mode of portlet view or edit
copyCurrentRenderParameters: This is whether you want copy all render parameters or not means if you make this tru parameter will available in complete request cycle.

Finally URL likes this:




p_p_id: current portlet id
p_p_state: windiow sate
p_p_mode: portlet mode either view/edit
p_p_lifecycle: this is life cycle of portlet
0: render phase or render URL
1: action phase or action URL
2: server resource URL
To pass custom parameters to this URL we need to use <liferay-portlet:param/> tag

Note:

If we not pass portletName and plid then URL will be created for current portlet.

Examples for create Liferay URLs from Liferay Portlet Tag Library:

Render URL:


<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<liferay-portlet:renderURL portletName="47"   var="openPortletURL" plid="<%=themeDisplay.getPlid()%>"  varImpl="openPortletURL" windowState="<%=LiferayWindowState.POP_UP.toString()%>">
<liferay-portlet:param name="name" value="meera"/>
</liferay-portlet:renderURL>

Action URL:

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<liferay-portlet:actionURL portletName="47"   var="actionURL" plid="<%=themeDisplay.getPlid()%>"  varImpl="openPortletURL" windowState="<%=LiferayWindowState.MAXIMIZED.toString()%>">
<liferay-portlet:param name="name" value="meera"/>
</liferay-portlet:actionURL>

Resource URL:

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<liferay-portlet:resourceURL   var="resourceURL" plid="<%=themeDisplay.getPlid()%>"  varImpl="openPortletURL" windowState="<%=LiferayWindowState.MAXIMIZED.toString()%>">
<liferay-portlet:param name="name" value="meera"/>
</liferay-portlet:resourceURL>

Using complete java classes and java objects

  • Create From renderResponse object
  • PortletURLFactoryUtil Class


Create From renderResponse object:

We can create URLs from render response object this is implicit variable we can directly us this. To make it available we need add following tag in jsp page then we can use renderResponse object in jsp page


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />


The following is sample code:

Render URL:


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%@page import="javax.portlet.PortletURL"%>
<%
PortletURL myRenderURL=renderResponse.createRenderURL();
myRenderURL.setWindowState(LiferayWindowState.MINIMIZED);
myRenderURL.setParameter("name", "meera");
%>


Action URL:


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%@page import="javax.portlet.PortletURL"%>

<%

PortletURL myActionURL=renderResponse.createActionURL();
myActionURL.setWindowState(LiferayWindowState.MAXIMIZED);
myActionURL.setParameter("name", "meera");

%>


Resource URL:


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%@page import="javax.portlet.ResourceURL"%>

<%

ResourceURL myReourceURL=renderResponse.createResourceURL();
myReourceURL.setResourceID("myResource");
myReourceURL.setParameter("name", "meera");

%>


Note: from renderResponse we can create URLs for only current portlet.

PortletURLFactoryUtil Class:

PortletURLFactoryUtil is util class from which we can create liferay URLs. Here we can create URL to other portlet also. Means from portletA we can create URL for portletB.

The following is examples:

Render URL:


<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletURL"%>
<%@page import="com.liferay.portlet.PortletURLFactoryUtil"%>
<%
LiferayPortletURL myRenderURL=PortletURLFactoryUtil.create(renderRequest, "47", themeDisplay.getPlid(), "0");
myRenderURL.setWindowState(LiferayWindowState.MINIMIZED);
myRenderURL.setParameter("name", "meera");
%>


Action URL:


<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletURL"%>
<%@page import="com.liferay.portlet.PortletURLFactoryUtil"%>
<%
LiferayPortletURL myActionURL=PortletURLFactoryUtil.create(renderRequest, "47", themeDisplay.getPlid(), "1");
myActionURL.setWindowState(LiferayWindowState.MAXIMIZED);
myActionURL.setParameter("name", "meera");
%>


Resource URL:


<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletURL"%>
<%@page import="com.liferay.portlet.PortletURLFactoryUtil"%>
<%
LiferayPortletURL myReourceURL=PortletURLFactoryUtil.create(renderRequest, "47", themeDisplay.getPlid(), "2");
myReourceURL.setParameter("name", "meera");
%>


Finding portlet name or p_p_id:

Go through following link and find How to find portlet Id or p_p_id or portlet Name
section.


Using Java script:

We can also create liferay URL from java script in liferay. Liferay provides java script module called liferay-portlet-url from this we can create URLs in java script
First we need to use AUI tag library then we have to use AUI use method to load liferay-portlet-url   module


<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:script>
AUI().use('aui-base','liferay-portlet-url','aui-node', function(A) {

});

</aui:script>

Examples:

Render URL:



<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:script>
AUI().use('aui-base','liferay-portlet-url','aui-node', function(A) {
var myRenderURL =Liferay.PortletURL.createRenderURL();
myRenderURL.setPortletId('47') 
myRenderURL.setWindowState('pop_up');

});
</aui:script>


Action URL:



<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:script>
AUI().use('aui-base','liferay-portlet-url','aui-node', function(A) {
var myActionURL =Liferay.PortletURL.createActionURL();
myActionURL.setPortletId('47') 
myActionURL.setWindowState('MAXIMIZED');  
myActionURL.setParameter("name","meera");
});
</aui:script>



Resource URL:


 
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<aui:script>
AUI().use('aui-base','liferay-portlet-url','aui-node', function(A) {
var myResourceURL =Liferay.PortletURL.createResourceURL();
 myResourceURL.setResourceId('someid');
 myResourceURL.setParameter("name","meera");});
</aui:script>




Note: all these code snippets use in jsp pages only.

Important points:

  • We can create Liferay URLs in different ways  from tag library java classes and java script.
  • From render response and portlet tag library we can create URL for only current portlet.
  • From PortletURLFactoryUtil class and liferay-portlet tag library we can create URLs to other portlets means from one portlet to other portlet we can create .Here main attributes are portletName and plid. Plid is page layout id.
  • When we create any URL finally url converted into normal URL query string which we can see in browser address bar.
  • Liferay portlet have different lifecycles action render and resource each phase we represend as p_p_lifecycle attribute.  1 for action phase, 0 for render phase and 2 for recourse phase.
  • Liferay portlet have edit and view mode.
  • When create URL using java script we have to liferay-portlet-url    module this module can be loaded by aui use method.


Author:
Meera Prince









Popular Posts

Recent Posts

Recent Posts Widget