Tuesday, 26 May 2015

How to use Liferay's FriendlyURL Mapper

Advantage: of FriendlyURL mapping is that it removes unwanted parameters from the URL and makes the URL very simple.

There are simple 3 steps to configure Liferay's FriendlyURLMapper in custom portlet

1) Configure friendly URL details inside "liferay-portlet.xml" file
2) Create Friendly URL routes mapping file
3)     Create Liferay URL in JSP
4)     Build your portlet & deploy it again and see URL being generated

Lets see in detailed what entries we need to specify for configuration:

1) Configure friendly URL details inside "liferay-portlet.xml" file

We need to specify 3 things as mentioned below:

<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>my-mapping</friendly-url-mapping>
<friendly-url-routes>com/mypage/portlet/my-portlet-friendly-url.xml</friendly-url-routes>

In most of the cases we will keep "DefaultFriendlyURLMapper" for the friendly-url-mapper-class.
"my-mapping" name after which all the custom parameters will be appended. For more details please see sample URL(step - 4) which gets generated.
"my-portlet-friendly-url.xml", this is the file in which we need to specify all the mapping configuration.

2) Create Friendly URL routes mapping file

<?xml version="1.0"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.0.0//EN" "http://www.liferay.com/dtd/liferay-friendly-url-routes_6_0_0.dtd">
<routes>
<route>
<pattern>/{myPageName}</pattern>
<generated-parameter name="myJsp">/{myPageName}.jsp</generated-parameter>
</route>
</routes>

3) Create Liferay URL in JSP

<portlet:actionURL var="getMyPageDetails" name="getMyPageDetails" >
<portlet:param name="myJsp" value="/sample.jsp"></portlet:param>
</portlet:actionURL>  

Make sure that parameter passed in Lifery URL in our case "actionURL" must match the "generated-parameter" name value of Friendly URL mapping file.

4) Build your portlet & deploy it again and see URL being generated

Here is the Sample URL:

Eariler (Before using Friendly URL):
http://localhost:8080/web/student/course?p_p_id=MyPage_WAR_MyPageportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=4&_MyPage_WAR_MyPageportlet_myJsp=%2Fsample.jsp&_MyPage_WAR_MyPageportlet_javax.portlet.action=getMyPageDetails

After applying Friendly URL changes:
http://localhost:8080/web/student/course/-/my-mapping/sample?p_p_lifecycle=1&_MyPage_WAR_MyPageportlet_javax.portlet.action=getMyPageDetails


We are done!

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics

Friday, 1 May 2015

How to use Liferay portlet namespace inside script tag

There are two ways available to use <portlet:namespace/> inside script tag / js file

1) Take hidden variable inside JSP and store value of portlet name space in it
2) Use script tag inside your JSP file and directly use <portlet:namespace/>

1) Take hidden variable inside JSP and store value of portlet name space in it

    <input type="hidden" name="nameSpaceValue" id="nameSpaceValue" value='<portlet:namespace/>' />

    Once it is declared like this inside .js file we can use portlet name space like this,
   
    var nameSpaceVal = document.getElementById('nameSpaceValue').value;
   
    document. + nameSpaceVal + fm.action = {some action}
   
2) Use script tag inside your JSP file and directly use <portlet:namespace/>

    function getStudentDetail() {
    document.<portlet:namespace/>fm.action='<%=getStudentDetails%>';
    document.<portlet:namespace/>fm.submit();
    }  


For debugging purpose, you can check the value of the function(getStudentDetail()) and how the values are getting formed for the lines

document.<portlet:namespace/>fm.action='<%=getStudentDetails%>';
document.<portlet:namespace/>fm.submit();

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics