5

I am new in Liferay. I used service builder to create database. I populated them manually. And eventually I could call them in JSP using PersonLocalServiceUtil class. Now I want to use Ajax in my program.

For example: Person with attributes personID, personWeight, personTemperature and datetime are stored in database.

I want to do an ajax call by personID to see his weight or Temperature graph for particular timeline. All I know, I can call list from Database to controller But I don't know How can I convert it to JSON obejct and forward it to JSP upon request, so that JSP won't refresh the page but display new graph.

Niranjan
  • 1,776
  • 1
  • 13
  • 21
NNN
  • 87
  • 1
  • 2
  • 7

2 Answers2

8

You can send person id from jsp using ajax request and you can fetch data inside controller(serveResource method) i.e

You need to create URL

<portlet:resourceURL var="resourceURL" >
</portlet:resourceURL>

Ajax Call

$.ajax({
  url : '${resourceURL}',
  data : data,//person id to sent
  type: 'POST',
  dataType : "json",
  success : function(data) {
    // get person data back from controller
  }
});

Controller part

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

    //get person id using getparameter 

    JSONObject json = JSONFactoryUtil.createJSONObject();

    json.put("jsonData", "person details");
    response.getWriter().write(json.toString());

}

HTH

Laxman Rana
  • 2,904
  • 3
  • 25
  • 37
  • Hi Before I close the answer, I just wanted to know one more issue. Why Service Builder is not generating JsonSerializer class ? And If It was generated, could I use it directly to response http requests in controller ?? – NNN Jul 22 '13 at 11:51
  • Thanks Lucky Boy! This is completely covers very common scenario. – Daddy32 Jul 08 '14 at 14:08
  • [**respond to http request with json object in portlet**](https://stackoverflow.com/questions/17592144/respond-to-http-request-with-json-object-in-portlet) – Parkash Kumar Jun 23 '17 at 12:08
0
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("test1", "test1test1test1test1test1");

    super.writeJSON(request, response, new Gson().toJson(data));
}