Liferay 7 friendly URL


Liferay 7 friendly URL

Liferay 7 Friendly URL tutorial will drive you creation of friendly URL in Liferay 7. Liferay generated URL’s for render,action and resource request are not readable and but liferay allows you make them friendly.

Liferay 6 Friendly URL High Level steps:

  • Create routes.xml file
  • Update liferay-porlet.xml with friendly URL configuration. Click on below tutoril for Liferay 6 version:

Friendly url configuration in Liferay 6

In this tutorial, we will learn on Liferay friendly url mappings. Liferay generates lengthy url’s for render,action url with below attributes: READ MORE

What changed in Liferay 7??

As you know, there is no liferay-portlet.xml, but still you can provide liferay additional properties to @Component in property element, but for friendly URL there is not config element to provide in property.

Liferay 7 High Level Steps for Friendly URL : 

  • Create routes.xml  (like what you did in liferay 6)
  • Create OSGI Service Component for Friendly URL.  Liferay uses OSGI Services to customize or extend,so you are required to create OSGI Service for your portlet component. In the @Component annotation,
    • javax.portlet.name :  Give the portlet name for which friendly URL’s mapped
    • com.liferay.portlet.friendly-url-routes :   Give the path of routes.xml
    • service :  Give FriendlyURLMapper.class and class need to extend DefaultFriendlyURLMapper.java
    • @Component(
       property = {
       "com.liferay.portlet.friendly-url-routes=META-INF/routes/routes.xml",
       "javax.portlet.name=org_javasavvy_web_leave_portlet"
       },
       service = FriendlyURLMapper.class
       )
      
      

Let’s Create Friendly URL for below  MVCRenderViewCommand:

  • Render Request in view.jsp :
    1. <portlet:renderURL var="rowURL" > 
       <portlet:param name="backURL" value="<%=currentURL %>" /> 
       <portlet:param name="leaveId" value="${leave.leaveId}" /> 
       <portlet:param name="mvcRenderCommandName" value="viewleave_info"/>
       </portlet:renderURL>
  • Generated URL is:
    • http://localhost:8080/web/guest/leave?p_p_id=org_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE
      &p_p_lifecycle=0
      &p_p_state=normal
      &p_p_mode=view
      &p_p_col_id=column-1
      &p_p_col_count=1
      &_org_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE_mvcRenderCommandName=viewleave_info
      &_org_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE_backURL=http%3A%2F%2Flocalhost%3A8080%2Fweb%2Fguest%2Fleave%3Fp_p_id%3Dorg_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE%26p_p_lifecycle%3D0%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_col_id%3Dcolumn-1%26p_p_col_count%3D1
      &_org_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE_leaveId=1
  • Generated Friendly URL is:
    • http://localhost:8080/web/guest/leave/-/leave/view/1?_org_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE_backURL=http%3A%2F%2Flocalhost%3A8080%2Fweb%2Fguest%2Fleave%3Fp_p_id%3Dorg_javasavvy_web_leave_portlet_INSTANCE_SgLAqlAWbjZE%26p_p_lifecycle%3D0%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_col_id%3Dcolumn-1%26p_p_col_count%3D1
  • Lets create project structure like below and create routes folder in src/main/resources/META-INF  folder like shown below:Liferay 7 Friendly URL router
  • Create routes.xml file in routes folder:
    • <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 7.0.0//EN" 
       "http://www.liferay.com/dtd/liferay-friendly-url-routes_7_0_0.dtd">
      <routes>
      <route>
       <pattern>/view/{leaveId}</pattern>
       <generated-parameter name="leaveId">{leaveId}</generated-parameter>
       <implicit-parameter name="mvcRenderCommandName">viewleave_info</implicit-parameter>
       <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
       <implicit-parameter name="p_p_state">normal</implicit-parameter>
       <implicit-parameter name="p_p_mode">view</implicit-parameter>
       <implicit-parameter name="p_p_col_id">column-1</implicit-parameter>
       <implicit-parameter name="p_p_col_count">1</implicit-parameter>
      
      </route>
      </routes>
  • Create LeaveRoutesMapper class like shown in below in package: org.javasavvy.leave.routes

 

package org.javasavvy.leave.routes;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;
import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
@Component(
 property = {
 "com.liferay.portlet.friendly-url-routes=META-INF/routes/routes.xml",
 "javax.portlet.name=org_javasavvy_web_leave_portlet"
 },
 service = FriendlyURLMapper.class
 )
public class LeaveRoutesMapper extends DefaultFriendlyURLMapper {
 
 @Override
 public String getMapping() {
 return "leave";
 }

}
  • LeaveRoutesMapper need to extend DefaultFriendlyURLMapper or implement FriendlyURLMapper class
  • override getMapping method and return the string  which you want to prefix URL:
    • Generated Friendly URL syntax: {context-url}/{page-url}-/{getMapping()/{pattern} 
    • http://localhost:8080/web/guest/leave/-/leave/view/101?
2 thoughts on “Liferay 7 friendly URL”
  1. Perfect, it was what I’m looking for my website. the only question itś if we try to make the same for an internal liferayś portlet like Asset Publisher portlet, the liferay IDE Friendly URL component only works with my own portlets. is there any way to work with friendly URL Asset Publisher?
    Many Thanks in advance
    Cheers

    1. The same way you are setting the portlet name on the friendly class component annotation, I guess you just need to create a component with the friendly url mapper that points to the AssetPublisher portlet name and set the required routes.

Comments are closed.