[button align=”left” color=”green” size=”small” link=”http://tech.learnerandtutor.com/read-json-object-from-struts-2-action-by-jquery-ajax”]All technical posts are migrated to www.learnerandtutor.com. Click here to go….[/button][clearline]
[button align=”right” color=”blue” link=”http://tech.learnerandtutor.com/read-json-object-from-struts-2-action-by-jquery-ajax/”]View in English[/button]
To read java objects from action class as JSON object please follow the steps as below.To understand completely please read this article first.
Modify the struts.xml as mentioned in the post.
To read the json just add the return type as JSON in the struts.xml action as below.
<action name="readJSON" class="com.rajesh.json.ReadJSON" method="readJSON"> <result type="json" /> </action>
When we put the above code, while calling the action from javascript it will call all the getter methods inside that class and bid as object then it will return to the call.
See the javascript code below.
function getJSONData() { console.log("Read JSON Data"); $.getJSON("readJSON.action", function (res) { console.log(res.data.length); for (var i = 0; i < res.data.length; i++) { console.log(" " + res.data[i].name + "-" + res.data[i].id + "-" + res.data[i].active + "-" + res.data[i].date); } }); console.log("Method Over"); }
When we execute the above code all the getters methods inside the action class will be invoked. (i.e) All the methods starting with word ‘get’ will be called. Apart from this the method that is mentioned in the struts.xml will also be called. In our example the method readJSON() will be called. No need to return any value. Just return the string “SUCCESS”. See the readJSON method in the below ReadJSON.java .
package com.rajesh.json; import java.util.ArrayList; import java.util.List; import com.rajesh.json.Report; import com.opensymphony.xwork2.ActionSupport; public class ReadJSON extends ActionSupport { private static final long serialVersionUID = -6765991741441442190L; private List < Report > data; public String readJSON() { System.out.println("getJSON Method Call Before"); data = new ArrayList < Report > (); Report obj = new Report(); obj.setActive(false); obj.setColor("Green"); obj.setDate("05-Sep-2013"); obj.setId(1); obj.setName("Rajesh"); this.data.add(obj); System.out.println("getJSON Method Call"); System.out.println("Length of Data is " + data.size()); try { for (int i = 0; i < data.size(); i++) { System.out.println("Color is " + data.get(i).getColor()); System.out.println("Date is " + data.get(i).getDate()); System.out.println("ID is " + data.get(i).getId()); System.out.println("Names is " + data.get(i).getName()); } } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public String writeJSON() { try { System.out.println(data.size()); for (int i = 0; i < data.size(); i++) { System.out.println("Data " + data.get(i).getColor() + "-" + data.get(i).getDate() + "-" + data.get(i).getId() + "-" + data.get(i).getName()); } System.out.println("Execute Method"); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public List < Report > getData() { System.out.println("Getter Call"); return data; } public void setData(List < Report > data) { System.out.println("Setter Call Flow"); this.data = data; } }
In the above code inside readJSON() method we are creating an object for Report class and assigned it to the local variable data. So when we call this action, this will set the data object and return success. As we mentioned the result type as json the getData methos will be called and the data object will be returned.
As mentioned in the javascript code we can access the data objects by the name like below.
res.data[i].name; res.data[i].color; res.data[i].Date;
To check the output see the output in the browser console. In our example we just displayed in the console. If you want to process just replace those portion as per the need.
Download the complete eclipse project here. Just import in eclipse by “Existing Project into Workspace” and run.
If you have any queries just send a mail to rajeshmepco@gmail.com. We will solve the problem.