wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 and JSON" Watch "Struts 2 and JSON" New topic
Author

Struts 2 and JSON

Pedro Fracarolli
Greenhorn

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?

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

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.


boo!
David Newton
Author
Rancher

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
Greenhorn

Joined: Sep 03, 2008
Posts: 20
Very interesting stuff. Thank you guys.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts 2 and JSON