Send JSON object to Struts 2 Action by JQuery AJAX

[button align=”left” color=”green” size=”small” link=”http://tech.learnerandtutor.com/send-json-object-to-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/send-json-object-to-struts-2-action-by-jquery-ajax/”]View in English[/button]
To send data from client side to server side, one of the best way is send as JSON object.
Please follow the steps below to achive this.

To do this first we have to enable json compatibility in the struts2 framework. For this modify the struts.xml file as below.

In the above code we added the json interceptor to the defaultStack. If you have your own stack add the lines below to your stack.

Next extend the json package to the default package as below

And the last change in the struts.xml file is set the return type as json for the request action.

That’s all the change for struts2.xml

In our example we are going to send data from the client side to server side. So we will create a json data object with javascript as below.

The JSON object syntax will be

In the above code, we created a variable dataObj which has only one variable data. But the variable “data” contains 4 objects. Each object has four variables namely “active”,”color”,”date”, “id” and “name”.

Now we will see how this is getting called in detail. First convert the created object into json object with the below code.

This line of code will convert the object to json string. If you want to know the difference before and after in detail display it in the console and view in the browser console window like below.

Then to send this data to the action write the javascript code below.

When we execute the above code, the struts framework will look the data object then it will find a variable with name “data”. So it will look for the method setData() method in the action class ReadJSON.java. If it is there it will run the code and try to parse the data.

See the action class ReadJSON.java below.

In the above code we have setData method. So it will try to put our JSON object as parameter for that method. But the variable “data” has array of objects as value. So we will create a java class with name Report and it will have “active”,”color”,”date”, “id” and “name” as it’s members. See below.

In Report.java we added getter and setter methods for each of the variables why because the struts framework by default will look for set method for each variable. As we mentioned the parameter as List<Report> data in the setData() method it will create an object of Report, then it will try to call setMethod for each variable like setColor(), setDate(),etc.,

That’s all. Now we will initiate the action from the html file as below.

To check the output see the output in the Eclipse 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.

Leave a Reply

Your email address will not be published. Required fields are marked *