In this example will see how we can get liferay portal users using serverResource url with alloy UI Ajax.
Step 1 : Create one portlet plugin project , portlet name in this example ajaxExamples, inside this portlet plugin project create one portlet
name alloyajax
Step 2 : Paste below code in portlet view.jsp
Step 1 : Create one portlet plugin project , portlet name in this example ajaxExamples, inside this portlet plugin project create one portlet
name alloyajax
Step 2 : Paste below code in portlet view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <portlet:defineObjects /> <portlet:resourceURL var="portalUsersDetailresourceURL"/> <h1 style="color : red;">Liferay Ajax Alloy UI Example 2</h1><br/><br/> <aui:a href="#" id="getPortalUsers" name="getPortalUsers" label="Click Here To Get Portal User With Detail"></aui:a><br/> <table id="portalUsersTable" border="1"></table> <aui:script> AUI().use('aui-base','aui-io-request', function(A){ A.one("#<portlet:namespace/>getPortalUsers").on('click',function(){ var UsersallRows=""; //aui ajax call A.io.request('<%=portalUsersDetailresourceURL%>', { dataType: 'json', method: 'GET', //Below using data part we can pass any parameter also.In this example we are not passing any parameter // data: { param: 'ParamValue' }, on: { success: function() { var data=this.get('responseData'); A.Array.each(data, function(obj, idx){ var UserstableRow='<tr><td>'+obj.screeName+'</td><td>'+obj.fullName+'</td><td>'+obj.email+'</td></tr>'; UsersallRows = UsersallRows+UserstableRow; }); A.one('#portalUsersTable').empty(); A.one('#portalUsersTable').set('innerHTML',UsersallRows); } } }); }); }); </aui:script>Step 3 : Paste below code in portlet controller class AlloyAjax.java
package com.test; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.portlet.PortletException; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import com.liferay.portal.kernel.json.JSONArray; import com.liferay.portal.kernel.json.JSONFactoryUtil; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.model.User; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.util.bridges.mvc.MVCPortlet; /** * Portlet implementation class AlloyAjax */ public class AlloyAjax extends MVCPortlet { @Override public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)throws IOException, PortletException { try { JSONObject jsonUserObject=null; JSONArray usersJsonArray=JSONFactoryUtil.createJSONArray(); List<User> userList = UserLocalServiceUtil.getUsers(-1, -1); System.out.println("list :::"+userList); for(User userObj:userList){ jsonUserObject = JSONFactoryUtil.createJSONObject(); jsonUserObject.put("fullName",userObj.getFullName()); jsonUserObject.put("email",userObj.getEmailAddress()); jsonUserObject.put("screeName",userObj.getScreenName()); usersJsonArray.put(jsonUserObject); } PrintWriter out=resourceResponse.getWriter(); System.out.println(usersJsonArray.toString()); out.print(usersJsonArray.toString()); } catch (Exception e) { e.printStackTrace(); } } }Output :
0 comments:
Post a Comment