Tuesday, November 18, 2014

Liferay Requires Name Spaced Parameters


When we work with liferay 6.2 portlet development some time we may get null or empty values when we access request parameters or form input values in the portlet action class.

The reason is from Liferay 6.2 Portlet Action class only consider the request parameters which should append with portlet name space value. This is default behavior of Liferay 6.2 Portlet Action Class.

Example:

ParamUtil.getString(uploadPortletRequest, "paramName") ===> empty value
actionRequest.getParameter("paramName") ===> null

Same behavior you can see for renderRequst, resourceRequest objects too while accessing request parameters or form inputs in the portlet action class respective methods.

What is Portlet Name Space?

Portlet Name Space will give unique name to the elements in the page and these elements are associated to respective portlet. This will avoid name conflicts in the page.
We may use many portlets within one page and there may be chance to get name collision among elements in the page. portlet name space will avoid those name conflicts in the page.

Each portlet have its own portlet name space value so that we need to append these portlet name space value to each element in the portlet like HTML tag names or ids and we can use this for request parameters.

How can i get Portlet Namespace Value?

We can use following two ways

  1. Using <portlet:namespace/> Tag.
  2. Using renderResponse Object

Using <portlet:namespace/> Tag.

We can use <portlet:namespace/> Tag so that we can append portlet name space value for element names,ids and request parameters.

Note :
we need to import respective tag library in the jsp page to use above tag.

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

Example Code Snippets:

<input type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/>

<div id="<portlet:namespace/>testDiv">this is div</div>

Using renderResponse Object

We can also use renderResponse object to get portet Name space value. renderResponse is liferay implicit object and  these Objects will be available in JSP page.

We need to use following tag to get portlet implicit objects and it will be available to JSP page.

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />

Example Code Snippets:

<input type="text" name="<%=renderResponse.getNamespace()%>employeeName" id="<%=renderResponse.getNamespace()%>employeeName"/>
<div id="<%=renderResponse.getNamespace()%>testDiv">this is div</div>


To avoid name conflicts among portlets element names and ids its recommended portlet name space for each element.


Liferay Requires Namespaced Parameters

Liferay 6.2 we have to append portlet Name space for every name of input element i.e. form input elements or request parameters names otherwise portlet action class ignore the parameters which does not have portlet name space to names. Due to this we will get NULL/Empty values in the portlet action class when we get parameters values from request objects.

Solutions

We must Use Portlet Name Space Value for Every form Inputs/Request parameters

Use <requires-namespaced-parameters> tag value “false” in liferay-portlet.xml


We must Use Portlet Name Space Value for Every form Inputs/Request parameters

When use forms in development we must use portlet name space for each form inputs. We need append portlet name space value to form input name attribute.

Example Code Snippets:

<input type="text" name="<portlet:namespace/>employeeName" id="<portlet:namespace/>employeeName"/>

OR

<input type="text" name="<%=renderResponse.getNamespace()%>employeeName" id="<%=renderResponse.getNamespace()%>employeeName"/>

OR

<aui:input name="employeeName" id="employeeName" label="Emplyee Name"/>

Note:

When we use aui form tags then portlet name space will be appended automatically and we need not required to append explicitly.

Use Request Parameters in Ajax

When we want to send request data using Ajax then we have to append portlet name space value to each request parameter then only portlet action class consider those request parameters and we can access request parameter values.

Liferay AUI Ajax Call

AUI().use('aui-base','aui-io-request', function(A){
//aui ajax call to get updated content
A.io.request('<%=updaContentURL%>',{
dataType: 'json',
method: 'GET',
data: {
<portlet:namespace/>employeeName:employeeName,
<%=renderResponse.getNamespace()%>employeeId:employeeId
},
on: {
success: function() {
// response data will be received here
}
}
});
});

jQuery Ajax Call

$(document).on('ready',function(){
//jquey aja methos to call server side content
$.ajax({
url: '<%=updaContentURL%>',
dataType: "json",
data:{
<portlet:namespace/>employeeName:employeeName,
<%=renderResponse.getNamespace()%>employeeId:employeeId
},
type: "get",
success: function(data){
//here response data will come and we need to update in the page.
},
beforeSend: function(){
// it will execute before Ajax call start
},
complete: function(){
// this method will execute after success method is completed.
},
error: function(){
}
});
});


Use '<requires-namespaced-parameters>' tag value “false” in liferay-portlet.xml

Instead of adding portlet name space value to each request parameters we can make false value to <requires-namespaced-parameters> in liferay-portlet.xml then portlet action class consider request parameters even it don't have portlet name space.

The following configuration we need to do in liferay-portlet.xml file

<requires-namespaced-parameters>false</requires-namespaced-parameters>


Finally you can access form inputs or request parameters in portlet action class so that we can get values

ParamUtil.getString(actionRequest, "paramName") ===>Some Value
actionRequest.getParameter("paramName") ===> Some Value

ParamUtil.getString(renderRequest, "paramName") ===>Some Value
renderRequest.getParameter("paramName") ===> Some Value

ParamUtil.getString(resourceRequest, "paramName") ===>Some Value
resourceRequest.getParameter("paramName") ===> Some Value

Author

0 comments :

Post a Comment

Recent Posts

Recent Posts Widget

Popular Posts