create render URL by Portlet Tag (portlet:renderURL) in JSP

portlet:renderURL is one of the custom tag provided by default portlet implementation (resides in portlet.jar).This tag is used to create render URL.
In this article, we will see how to create render URL by portlet tag. Sun provides implementation of portlet API in portlet.jar file. This jar file contains classes and interfaces under javax.portlet pacakage.
This implementation (given in portlet.jar file under tomcat/lib/ext path for Liferay server) provides certain custom tags to create Render URL. Render URL is used to call render() phase / lifecycle method of portlet.
render URL by Portlet Tag portlet:renderURL
First we will create LiferayMVC portlet. You can refer blog on Creating MVC Portlet in Liferay to create project structure. I gave plugin project name as renderURL-by-portletTag and -portlet will be appended by Liferay IDE while creating Liferay plugin project. I gave portlet class as com.opensource.techblog.portlet.RenderURLByPortletTagPortlet 

render URL by Portlet Tag (portlet:renderURL)

Once portlet plugin project is created by eclipse (Liferay IDE), it will create view.jsp under docroot folder. We will add portlet tag in this jsp. To avail this tag in view.jsp, add following taglib definition.


While creating portlet with Eclipse (Liferay developer studio), this will be added by default at the time of project creation (Smart work done by eclipse).

Once this is done, add following code in view.jsp


Explanation:-

  • <portlet:renderURL> tag is used to create render URL. This render URL is pointing to portlet in which its created (same portlet).
  • var attribute of <portlet:renderURL> tag is used to define a variable that holds the generated renderURL. This variable is available in page scope so that it can be accessed by EL (Expression Language) anywhere in JSP.
  • In above case, renderUrl variable holds generated renderURL.
  • windowState represents current state of portlet. following are possible window state
    • Maximize,Minimize, Normal,POP_UP
  • PortletMode represents current portlet mode. 
    • Following are possible portlet modes
      • Modes defined by portlet specification and provided by default implementation (portlet.jar)
        • View,Edit,Help
      • Additional modes provided by Liferay.
        • Configuration,About,Edit_Default,Edit_Guest,Preview,Print
  • copyCurrentRenderParameters :- This attribute decide if all render parameters are available in complete request cycle. By default its false. This is more understandable for action URL.
  • Additional parameters can be passed with <portlet:param> tag. Such parameters are accessible by renderRequest.getParameter API in portlet’s render method.
  • At the end, we have created link by anchor tag and set its href to renderUrl variable ( created by <portlet:renderURL> tag which holds renderURL ) through EL(Expression Language). 
  • You need to add following import in view.jsp

 

render() method called by render URL

I have override render() method as per code below. We are retrieving request parameter added by <portlet:param> into render URL.
 

Explanation:-

  • additional parameter we have added in renderURL (by <portlet:renderURL>) can be accessed with request.getParameter method. 
  • There is another way to read the parameters. Liferay provides ParamUtil class to access request parameter. This class has many convenience methods which are used to get parameter value in desire output like,
    • getBoolean, getFloat, getString etc
    • These methods also facilitate to pass default value at the end. In case if parameter is not exist, whatever default value we set will be returned (instead of null value)
  • In our case request.getParamer and ParamUtul.getString both are similar (will return same value).

Save portlet class, view.jsp and deploy the portlet. Refresh the page on which this portlet is placed and you will see link (that we created above) 
render URL by Portlet Tag (portlet:renderURL) in JSP - render URL in portlet

  • On refreshing this page, we will get following logs in server console
  • During the page refresh, render() method is called directly. At this time, there are no parameters passed so we will not get any parameters. 
  • You can observe that request.getParameter method returns null in case the parameter is not exist while ParamUtil.getString method simply returns empty string (Default value “”). 
  • ParamUtil.getString method will make developer life easy by protecting us from NullPointerException if parameter is not exist and we try to perform any operation on that.
  • Now click the link shown above and you will get below logs in server console.
  • This time, render() method is called by renderURL so we are getting value of additional parameter in console.
  • By inspecting element in browser, you will get follwing renderURL
    • http://localhost:8080/web/guest/test?p_p_id=renderURLbyportletTag_WAR_renderURLbyportlet&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_renderURLbyportletTag_WAR_renderURLbyportlet_param=Nilang
    • Explanation:-
      • p_p_id = unique portlet Id generated by portal. This is the key used to identify which renderURL points to which portlet.
      • p_p_lifecycle= Represent current Lifecycle of portlet. Following are possible lifecycles
        • 0 – render phase
        • 1 – action phase
        • 2 – serve resource phase
        • We are currently in render phase so its showing 0.
      • p_p_state= Represent current window state of portlet.
      • p_p_mode= Represents current mode of portlets.
      • p_p_col_id= Represent in which column this portlets is placed. In liferay we can define multi column layout. This parameter helps to find exact column on which this portlet is placed.
      • p_p_col_count= Represents total columns count for layout of current page on which the portlet is placed.
      • _portletphaselifecycle_WAR_portletphaselifecycleportlet_param1= This is the additional parameter name (param) that we have added while creating renderURL(through tag). You can observe that portlet namespace (p_p_id) is attached before each parameter name. This is useful if more than one portlets are placed on same page with same parameter name. In this case they all are uniquely identified with portlet name space attached before parameter name (Each portlet will have unique name space).

Summing Up

  • render URL can be created by portlet Tag(portlet:renderURL).
  • This tag is part of default portlet specification implementation (portlet.jar).
  • We can set portlet mode and window-state while creating render URL
  • We can pass additional parameter by <portlet:param> child tag inside <portlet:renderURL>
  • renderURL created by <portlet:renderURL> tag is only pointing to portlet that creates it.
Following are other possible ways to create render URL in Liferay
I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.
Download Source
Download Source

Share This Post

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.