Wednesday 9 September 2015

Reading the parameters from URL in Liferay

Reading the parameters from URL in Liferay

In Liferay some times we will face an issue to read the parameters from the URL (it may be actionURL or renderURL), it will give null value when you try to read the parameters with simple 'actionRequest' or 'renderRequest' in such scenario we need to read the parameters from the 'OriginalServletRequest'. the below example clearly shows how to read those parameters.

imports :-
import javax.servlet.http.HttpServletRequest;
import com.liferay.portal.util.PortalUtil;
import javax.portlet.ActionRequest;

java code :-

HttpServletRequest originalRequest  = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(actionRequest));

String STATUS = originalRequest.getParameter("STATUS");

//actionRequest for action method and renderRequest for rendermethod.

jsp code :- suppose you need to read in your jsp page then read the parameter in the following way.

HttpServletRequest originalRequest  = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));

String STATUS = originalRequest.getParameter("STATUS");

Note:- Suppose we are reading the parameters from the URL which are passing by any third party server(ex: payment gateways) in such type of scenarios, we need to read the parameters in the above way.

Locale message in javascript alert, liferay

As continue to my previous blog, sometimes we get requirement to display locale message in 'Notifications' or 'Alerts'. It is easy to diplay in jsp page in labels but how to display in alerts then the below is an example which will help you on this type of requirement.

example :-

function modeAlert(){
        alert("<liferay-ui:message key="payment"/>");
    }

Path for Language properties file :-  src/content/Language_en_US.properties

Language_en_US.properties :-  ex: (Key : Vlaue)
  
  payment: Payment 

Language_ms_MY.properties :-  ex: (Key : Vlaue)

  payment: Pembayaran

When ever you use the above key(first way or second way) depending upon the current locale the value will be diplayed.

WEB-INF/Portlet.xml :- The entry in the portlet.xml is also mandatory then only you can able to see the values otherwise you can see only key as output.

<portlet>
  <portlet-name>request</portlet-name>
  <display-name>Request</display-name>
  <portlet-class>com.Request</portlet-class>
  <init-param>
  </init-param>
  <supports>
  </supports>
  <supported-locale>en_US</supported-locale>
<supported-locale>ms_MY</supported-locale>
<resource-bundle>content.Language</resource-bundle>
  <portlet-info>
  </portlet-info>
 </portlet>

Different ways of reading keys from Language.propertis file in Liferay

In Liferay we have one great advantage is that, we can use the same key to get the different values of particular word or key based upon the Locale(nothing but the language). This blog will help you using the different ways.

1.Directly using tag :-

<liferay-ui:message key="payment"/>

2.By using Stirng Variable :-

String checkout = LanguageUtil.get(portletConfig,themeDisplay.getLocale(),"payment"); 

Path for Language properties file :-  src/content/Language_en_US.properties

Language_en_US.properties :-  ex: (Key : Vlaue)
  
  payment: Payment 

Language_ms_MY.properties :-  ex: (Key : Vlaue)

  payment: Pembayaran

When ever you use the above key(first way or second way) depending upon the current locale the value will be diplayed.

WEB-INF/Portlet.xml :- The entry in the portlet.xml is also mandatory then only you can able to see the values otherwise you can see only key as output.

<portlet>
  <portlet-name>request</portlet-name>
  <display-name>Request</display-name>
  <portlet-class>com.Request</portlet-class>
  <init-param>
  </init-param>
  <supports>
  </supports>
  <supported-locale>en_US</supported-locale>
<supported-locale>ms_MY</supported-locale>
<resource-bundle>content.Language</resource-bundle>
  <portlet-info>
  </portlet-info>
 </portlet>



Pagination Using JQuery dataTable in Liferay

Some times we will get the requirement to develop the pagination with better UI which can not be achievable using Liferay Search Container.for those requirement below example will be help to develop the pagination using JQuery.

Imports rquired for pagination :- 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

<script type="text/javascript" src="http://cdn.datatables.net/plug-ins/725b2a2115b/integration/bootstrap/2/dataTables.bootstrap.js"></script>

Html Code :-

<table class="table table-striped error" id="viewDataTables">
<thead>
<tr>
<th><liferay-ui:message key="no" /></th>
<th><liferay-ui:message key="name" /></th>
</tr>
</thead>

        <tbody>
                <tr>
<td><liferay-ui:message key="123" /></td>
<td><liferay-ui:message key="Imthiyaz" /></td>
</tr>
        </tbody>
 </table>

Note :- Here <thead> , <tbody> declaration is mandatory without these pagination will not work .

Script Code :- 

 <script type="text/javascript">

 $('#viewDataTables').dataTable({
     "bFilter" : false,
        "bLengthChange" : false,
        "iDisplayLength" : 10,
        "bSort" : true,
        "sDom" : 't <p> <"bottom"><"clear">'
    
});

 </script>  

Get Drop Down Value in Java Script Function in Liferay

Sometimes we will face an issue to read the drop down values in java script function, then the below code may be use full.

Note :- In chrome browser onclick function will not work.

<aui:select name="selectvideo" id="selectvideo1" label="Select Video" onChange="getPrice()">
           <aui:option value="-1" >
                   <liferay-ui:message key="select-video" />
               </aui:option>


<script>
function getPrice() {
var sel = document.getElementById("<portlet:namespace/>selectvideo1");
document.getElementById("<portlet:namespace/>key").value = sel.options[sel.selectedIndex].value;
}
</script>

Call Action URL using javascript function in Liferay

Sometimes we will get a requirement that we need to hit the "Action URL" using java script function.
May be my blog will be help you fot that requirement.

   <%
           PortletURL actionURL = renderResponse.createActionURL();
           actionURL.setParameter(ActionRequest.ACTION_NAME, "getPrice");
    %>
// creating action URL which will hit the Action method called "getPrice".

<aui:form name="fm" method="POST" >

<aui:input type="text" name="price" label=""  required="true"  onclick="getPrice()" inlineLabel="true">
                                    <aui:validator name="number"></aui:validator>
                                </aui:input>

<aui:button-row>
                    <aui:button type="submit" for="Submit" key="submit" value="Submit"/>                 </aui:button-row>
</aui:form>

// I have created one form with one text field  when i click that field i need to call getPrice action URL

<script>
        function getPrice() {
            document.<portlet:namespace/>fm.action = '<%=actionURL.toString()%>';
            document.<portlet:namespace/>fm.submit();
        }

    </script>

// The above javascript code is usefull  to hit the aciton URL with javascript function without clicking the submit button through javascript we are clicking the submit button.

Hope this is helpfull for you

Properties Entry while working with Ajax calls in Liferay

Some times Ajax call will not work properly in liferay,  when you are passing parameters you will face some issues like not able to pass the values through Ajax call then add the following tags in liferay-portlet.xml
<requires-namespaced-parameters>false</requires-namespaced-parameters>
  <ajaxable>true</ajaxable>

No comments:

Post a Comment