File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts 2 and JSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 and JSON" Watch "Struts 2 and JSON" New topic

Struts 2 and JSON

Pedro Fracarolli

Joined: Sep 03, 2008
Posts: 20
Hi guys.

Does Struts 2 have support for sending JSON HTTP responses natively? I mean, I can use the println() method from the HttpServletResponse output stream when it comes to Struts 1.2.x in order to generate a JSON response, but, in my opinion, this is not elegant.

If there's no "native" support, would there be a plugin or something?

[ November 11, 2008: Message edited by: Bear Bibeault ]
Scotty Boy Sinclair

Joined: Sep 11, 2008
Posts: 5
Struts 2 has a JSON result type which you can use by adding the correct plugin JAR.

You then just declare a result type as follows

The parameter "root" should point to the getter property of the action object which contains the object to convert to JSON data.

The plugin uses reflection to convert any object to JSON string data.

David Newton

Joined: Sep 29, 2008
Posts: 12617

In addition to the afore-mentioned JSON result the REST plugin of Struts 2.1 also supports JSON responses, in addition to XML and XHTML, based on the requesting URI.

In other words, if we request we'd get JSON back, if we request we'd get XML back. We can also set the default response type so we can leave off the extension altogether.
Pedro Fracarolli

Joined: Sep 03, 2008
Posts: 20
Very interesting stuff. Thank you guys.
I agree. Here's the link:
subject: Struts 2 and JSON
jQuery in Action, 3rd edition