Laliwala IT Services

Laliwala IT Services
Website Development

Thursday, March 31, 2011

Liferay tutorial json : call service api "Liferay tutorial version 5.2"

 Liferay JSON tutorial
Liferay services layer
Liferay version 5.2.x
Liferay remote method


===============================================================
Invokining service layer thru JSON.
===============================================================

pre-requisite for JSON to work, we should have a remote method.

1. look into the following file

ext-web/docroot/html/js/liferay/ext_service_unpacked.js

Note: In liferay version 5.2.x onwards this file is "ext_service.js"

Note: in the earlier versions the file name is ext_service_unpacked.js

2. define a new java script function in view.jsp

< script src="/html/js/liferay/ext_service.js">< /script>

< script>
function < portlet:namespace />addBookThruJSON() {

var frm = document.< portlet:namespace />fm;
var title = frm.< portlet:namespace />book_title.value;
var params = {
title:title
};
Liferay.Service.Library.Book.create(params);
alert('Book successfully added');
frm.< portlet:namespace />book_title.value = "";
}
< /script>

3. Update the form,

Add another button to the form which when clicked will invoke addBookThruJSON() javascript method.

< input type="button" value="Add thru JSON" onClick="< portlet:namespace />addBookThruJSON();">

Now do "ant deploy-fast" from ext-web and see adding of the book thru json is working perfectly fine.

in the next example we'll see how to write a call back.





1 comment: