• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2 and JSON

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting stuff. Thank you guys.
 
reply
    Bookmark Topic Watch Topic
  • New Topic