Friendly URLs for Custom Portlets

Friendly URLs for Custom Portlets


Default Liferay's URLs are too long and do not look nice. You may want to make them better. And FriendlyURLs mechanism may help here.
Let's say, you have a custom portlet displaying a single entity (called 'app'), using the ID to fetch it ('appId'). So, we would like to have an URL like:
http://localhost:8080/web/guest/app-page/-/app/1234
, where 1234 is the appId.
Such URL is shorter and cleaner than the full URL with long namespaced parameters.
Below are the steps to register a simple friendly URL for custom portlet:
  • Create file src/main/resources/META-INF/friendly-url-routes/routes.xml in your portlet module:
    <?xml version="1.0"?>
    <!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 7.1.0//EN" "http://www.liferay.com/dtd/liferay-friendly-url-routes_7_1_0.dtd">
    <routes>
       <route>
           <pattern>/{appId:\d+}</pattern>
           <implicit-parameter name="mvcRenderCommandName">/app/view</implicit-parameter>
           <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
           <implicit-parameter name="p_p_state">normal</implicit-parameter>
       </route>
    </routes>
    
This will register appId parameter for the friendly URL.
  • Add parameter to public-render-parameters:
    @Component(
      immediate = true,
      property = {
         "javax.portlet.name=" + MyAppPortletKeys.PORTLET_ID,
         //…,
         "javax.portlet.supported-public-render-parameter=appId"
      },
      service = Portlet.class
    )
    public class FiscalProfilePortlet extends MVCPortlet {
    
    }
    
  • Create a FriendlyURLMapper class:
    @Component(
        property = {
            "com.liferay.portlet.friendly-url-routes=META-INF/friendly-url-routes/routes.xml",
            "javax.portlet.name=" + MyAppPortletKeys.PORTLET_ID
        },
        service = FriendlyURLMapper.class
    )
    public class AppDisplayFriendlyURLMapper extends DefaultFriendlyURLMapper {
        @Override
        public String getMapping() {
            return _MAPPING;
        }
        private static final String _MAPPING = "app";
    }
    
Here we define the mapping URL part, which goes after friendly URL separator (/-/):
http://localhost:8080/web/guest/app-page/-/app/1234
  • Create an MVCRenderCommand class, where you'll receive the parameter from friendlyURL:
    @Component(
        immediate = true,
        property = {
            "javax.portlet.name=" + MyAppPortletKeys.PORTLET_ID,
            "mvc.command.name=/",
            "mvc.command.name=/app/view"
        },
        service = MVCRenderCommand.class
    )
    public class AppDisplayMVCRenderCommand implements MVCRenderCommand {
    
        @Override
        public String render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException {
            long appId = ParamUtil.getLong(renderRequest, "appId");
        }
    
    }


Hope, this will help 😏

Comments

Popular posts from this blog

Liferay Search Container Example

Liferay DXP - max upload file size

Liferay Keycloak integration