Friendly URLs in Liferay

By default, Liferay portlet URLs contain many parameters like portlet mode, portlet life cycle, etc., which makes URLs very lengthy and complex. Sometimes we want to share a friendly 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 custom URL pattern along with important parameters and hiding unwanted parameters from the URL path.

Here is an example of implementing the friendly URLs feature in Liferay.

Implementing the Liferay Friendly URLs

Create a new Liferay portlet and add the following tags inside liferay-portlet.xml after the <icon> tag.

The configuration here defines the logic for how to map friendly URLs to regular Liferay URLs.

We can use Liferay’s DefaultFriendlyURLMapper class to add a friendly URL prefix.

The <friendly-url-routes> tag specified in the XML file defines the friendly URL routes.

<portlet>
 <portlet-name>friendly-url</portlet-name>
 <icon>/icon.png</icon>
 <friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
 <friendly-url-mapping>my-url</friendly-url-mapping>
 <friendly-url-routes>com/asb/urlContent/my-url-routes.xml</friendly-url-routes>
</portlet>

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" 
"http://www.liferay.com/dtd/liferay-friendly-url-routes_6_1_0.dtd">
<routes>
 <route>
 <pattern>/{myParam:\d+}/{pagename}</pattern>
 <generated-parameter name="mvcPath">/html/friendlyurl/{pagename}.jsp</generated-parameter>
 </route>
</routes>

Here, the <pattern> tag defines the friendly URL pattern. Each Liferay friendly URL will contain its own <route> tag with associated parameters.

Friendly URLs start with / followed by the defined prefix and URL pattern.

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.


Create two jsp pages to test the friendly URLs. For example, view.jsp and secondPage.jsp

Add the following code to the secondPage.jsp file.

Here we are sending a parameter called “myParam” through a friendly URL and printing it 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Insert title here</title>
   </head>
   <body>
      <h2>secondPage.jsp</h2>
      <%String param = request.getParameter("myParam");%>
      <h1><%=param %></h1>
   </body>
</html>

Add the below code in the view.jsp file.

<%@ taglib uri="http://java.sun.com/portlet_2_0" 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 the following friendly URLs:

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

Testing the output

We can access the portlet using the friendly URL pattern as shown below.

friendlyurl1

Click on the link and once the URL is redirected, check the URL pattern.

friendlyurl2

Conclusion

In this article, we learned about Liferay friendly URLs.

Friendly URLs in Liferay
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading