This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2 and JSON

 
Pedro Fracarolli
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting stuff. Thank you guys.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic