Portlet Request Types and Request flow sequence

Before talking about the portlet request types and its flow cycle, lets first briefly talk about the portlets and portal. Portlets are the building blocks in the portal environment. Portal server provides the portlet container, the environment to run the portlets and also provides the capability to create the page by aggregating the multiple portlets together.

When a Portal page is requested, many portlets are usually invoked and the mark-up fragment from each portlet is combined within the Portal’s page mark-up. As portlet is not the complete page, it is just a fragment of the page. Flow of the request for the portlet is different from the servlet. Portlets have three different types of the request.

  1. Render Request
  2. Action Request 
  3. Resource Request

Just for the explanation purpose consider a simple page with two portlets PortletA and PortletB dropped on it, as shown below.

portal-page

Render Request is called for rendering of the page. Sequence diagram shown below shows the request flow for render request. When a render request goes to the portal server. Portal server calls the rendering logic of each portlet to get the HTML fragments and then combines all the HTML fragments to form the complete and returns the complete page HTML to browser.

Portet_Render_Request

Action Request is called for performing operation. for example database update, calling back end service. Sequence diagram shown below shows the request flow for action request. When a action request goes to the portal server. Portal server calls the action logic of the portlet to perform the back-end operation and then calls the render logic of all the portlets, as it do in the render request and returns the page to the browser.
Portet_Action_Request

Resource Request is called for fetching the specific resource. Sequence diagram shown below shows the request flow for resource request. When a resource request goes to the portal server, Portal server just calls executes the resource method of portlet for which request is coming and returns the response. In some scenarios, it is required to fetch the specific HTML fragment instead of the complete page. for example in case of AJAX calls, we want specific HTML/JSON data to be returned.
Portet_Resource_Request

Converting struts 2 regular web application into struts 2 portlet

For converting the strut2 based regular web application into struts 2 portlet, following things will be required.

Add Configuration Files
Some portlet specific configuration needs to be added.

Update Web.xml
Struts 2 uses the filter as controller. The following filter configuration needs to added in the web.xml for controller.

 <filter>
<filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

Add the Struts2PortletDispatcherServlet configuration in the web.xml file for accessing the Struts2 value stack through request attributes and JSP tags.

  <servlet id="Struts2PortletDispatcherServlet">
    Struts2PortletDispatcherServlet
    org.apache.struts2.portlet.dispatcher.DispatcherServlet
  

For using the portlet tag libs, add the following tag lib configration.

  	
  		http://java.sun.com/portlet_2_0
  		
  			/WEB-INF/tld/liferay-portlet.tld
  		
  	

Add the Portlet configuration in the portlet.xml file and place this file in the WEB-INF folder of the web application. portlet.xml configuration file will contain the configuration for each portlet. Configure the org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher class as a portlet-class. To define the default landing page action of the portlet, viewNamespace and defaultViewAction. Following is the sample configuration for portlet which needs to be added in portlet.xml for each portlet.

	
		portlet-name
		Portlet display name Portlet
		org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher

		<!-- The namespace for the actions configured for view mode -->
		
		   viewNamespace
		   /view
		

		<!-- The default action to invoke in view mode. -->defaultViewActionindex0text/htmlenPortlet title 
			Portlet short title
			Portlet short tile
		
		
			administrator
		
		
			guest
		
		
			power-user
		
		
			user
		
	

Liferay Configuration Files

Add the following Liferay configuration file. For each portlet, configuration needs to added in these files.

  • liferay-portlet.xml
  • liferay-display.xml.

The liferay-porlet.xml file contains Liferay specific portlet configuration. It contains the path of portlet specific javascript, css files and portlet is instance able or not. The sample configuration is as follows:

	
		portletname
		/icon.png
		false
		<header-portlet-css>/css/main.css
		/js/main.js
		qml-ssp-portlet
	

Configuration in the liferay-display.xml file is responsible for showing the portlet in the Add Application section in the Liferay control panel:


	<category name="cat.portlet">
		
		
                ..................
	</category>
</display>

Struts configuration

The struts.xml file change package extends the configuration from struts-default to struts-portlet default as shown below.

<package name="default" extends="struts-portlet-default" abstract="true">

This allows actions of portlet to be grouped.  Actions belonging to a portlet will be moved into the package and namespace for the portlet.

JSP customisation

As we are converting a regular web application into portlet, Regular web application serves the complete page. But portlet serves only the fragment of the page. All the common element of the page needs to be removed from the portlet jsp file.

CSS/JavaScript

Remove all the common CSS and JavaScript import statements from JSPs and moved to the Liferay theme. Only portlet specific JavaScript and CSS will be included in the JSP files.

Remove Header and Footer imports

Remove the Header and footer html form the portlet JSP page. It should go in the theme file. In the Liferay portal, Theme builds the common structure of the page. All the common element should go into the theme.

Update Tag libs

Some of the taglib needs to be modified to work in the portlet environment.

For example, URLs in the portlet environment are handled quite differently to URLs in a web application. In the portlet environment, there is no such thing as an url extension (e.g *.action). In addition, there are some additional tag attributes that are portlet specific, such as portletURLType which specifies whether it is a render url or an action url.

Using the international character-set in Liferay web content

If you are planning to use the international characters in the Liferay web content, Liferay Database encoding needs to be set to UTF-8.

ISO encoding is ok, if you are just thinking to use the English Western character.

It is quite important to set the right database encoding before creating the schema and table for Liferay.

Liferay : Embed the page title in Layout template

If you want to show the page title as heading on each page. Better would be to embed the page title in layout template. Following is the code snippet for getting the page name which needs to be added in the layout template.
Continue reading “Liferay : Embed the page title in Layout template”

Liferay Portlet : Add response header for disabling caching

In the portlet, Caching can be disabled by setting the following response headers :

  • Cache-Control
  • Pragma
  • Expires

Above response headers can be set using the renderResponse object or html tags. Following are the code snippet for the same.
Continue reading “Liferay Portlet : Add response header for disabling caching”

Apache web server : allowing access to specific URLs outside LAN

When publishing the website in Liferay, it’s good practice to restrict access to admin urls outside the LAN. you can block the access to urls by adding the rewrite rules on the web-server. In my case, I am using the apache webserver and following are apache configurations for the same.

httpd.conf Continue reading “Apache web server : allowing access to specific URLs outside LAN”

Liferay Portlet : trun off portlet namespace prefixed to input field

When we do the portlet development in the Liferay, each input field name and request parameters needs to be prefixed with the portlet namespace.

Portletnamespace prefix is used avoid the conflict, if multiple portlets are dropped on the same page. But if you want to make your portlet work without portlet namespace prefix, it can be done by setting the requires-namespaced-parameters to false in the liferay-portlet.xml file.

<portlet>
	<portlet-name>customportlet</portlet-name>
	<icon>/icon.png</icon>
	<instanceable>false</instanceable>
	<requires-namespaced-parameters>false</requires-namespaced-parameters>
</portlet>