By default, portlet URLs contains lot of parameters like portlet mode,portlet life cycle,etc, which makes URL very lengthy and complex. Sometimes we want to share URL, which is human readable and meaningful. Also we may not want to expose all those parameters in the browser.

Liferay provides a friendly URL mapping capability, where we can define our own URL pattern along with important parameters and hiding unwanted parameters from the URL path. Here is an example of using friendly URL in liferay.

  • Create a new liferay portlet. Add the following tags inside liferay-portlet.xml after <icon> tag. Here defines the logic for how to map friendly URL to regular liferay URLs. In most cases we use liferay’s DefaultFriendlyURLMapper class. is used to add a prefix, that is appended in the friendly URL. tag is used to specify the xml file, which defines the friendly URL routes.
  • Now create a xml file(my-url-routes.xml) inside src directory (under com/asb/urlContent package)and add the following content.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.1.0//EN" 

 <generated-parameter name="mvcPath">/html/friendlyurl/{pagename}.jsp</generated-parameter>
  • In the above code,  is used to define a friendly URL pattern. each URL pattern will contain it’s own tag with associated parameters.
  •  specifies a parameter that should be ignored and not included in generated URLs. Ignored parameters do not effect URL recognition.
  •  specifies a parameter that is not present in the route pattern.
  •  specifies the pattern of a parameter that will be generated from other parameters when a URL is recognized. When a URL is built, these virtual parameters will be parsed from the generated parameter and made available in the route pattern for constructing the URL.
  •  specifies the pattern of the mapped friendly URL. Here /{myParam:\d+}/{pagename}/ specifies that, friendly URL contains a parameter named myParam, which is a number and followed by a string which is a jsp page.
  • Friendly URL starts with -/ followed by the defined prefix and URL pattern.
  • Create two jsp pages to test the friendly URLs.For example, view.jsp and secondPage.jspAdd the following code to seconPage.jsp. Here we are sending a parameter called “myParam” through friendly URL, and printing in seconPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%String param = request.getParameter("myParam");%>
<h1><%=param %></h1>
  • And add the following in view.jsp:
<%@ taglib uri="" prefix="portlet" %>
<portlet:defineObjects />

This is <h1>View.jsp</h1>
<a href="web/guest/home/-/my-url/secondPage/" >Go to second page</a>
  • These jsp files can be accessed now using following friendly URLs:

http://localhost:8080/web/guest/home/-/my-url/5/secondPage and, http://localhost:8080/web/guest/home/-/my-url/view

  • Following are the output screenshots:




Check out all Liferay related posts.

Note: Version used: Liferay 6.2 + Apache Tomcat 7.