Read JSON object from Struts 2 Action by JQuery AJAX

[button align=”left” color=”green” size=”small” link=””]All technical posts are migrated to Click here to go….[/button][clearline]
[button align=”right” color=”blue” link=””]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.

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.

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 .

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.

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  We will solve the problem.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.