Instantly share code, notes, and snippets.

Embed
What would you like to do?
MVCPortlet for Ajax Call
package com.ys.foo.portlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class
*/
public class MyMVCPortlet extends MVCPortlet {
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws PortletException, IOException {
resourceResponse.setContentType("text/javascript");
//Print What you get from Server
System.out.println("param1 sent from Browser- " + resourceRequest.getParameter("param1"));
System.out.println("param2 sent from Browser- " + resourceRequest.getParameter("param2"));
JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
ArrayList<String> strList = new ArrayList<String>();
strList.add("FIRST ONE");
strList.add("SECOND ONE");
//Send Data Back
jsonObject.put("retVal1", "Returing First value from server");
jsonObject.put("retVal2", strList.toString());
PrintWriter writer = resourceResponse.getWriter();
writer.write(jsonObject.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment