Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Name

struts2jsonresult

Publisher

Li Chang Geng Image Added

License

Apache License 2.0

Version

0.99(release candidate)

Compatibility

Struts 2.0.2+

Homepage

http://code.google.com/p/struts2jsonresult/

Download

http://code.google.com/p/struts2jsonresult/downloads/list

...

{rate:title=Rating|theme=dynamic|key=MyPlugin}

Overview

This struts2 plugin simply provide a new result type: 'json', as an alternative to struts2 json plugin. It provide better flexibility to specify what properties of a Java Bean to be serialized, and it works well with spring and hibernate.

...

In struts2.xml, define a package that extends struts-default and set the result type json as default.

...

Define your action in your struts2.xml

...

...

And the java code of your Action file:

...

...

Patterns

You can set the parameter named "patterns" to include or exclude some properties of the bean to be serialized.

Use '+' as include, and '-' as exclude.
For example

...

...

the patterns will be translated to

...

...

About how to use patterns in flexjson, refer to http://flexjson.sourceforge.net/ and the javadoc of JSONSerializer

...

alert({ ....});

Parameter name

Default value

Description

target

null

the bean to be serialized, if set to null, the action itself will be serialized.

patterns

null

Fully support the patterns that can be applied to flexjson. See the above section about "Patterns"

prettyPrint

false

whether to format the result json string

rootName

null

if set, it will wrap the resulting JSON in a javascript object that contains a single field named rootName.

deepSerialize

false

whether to deep serialize the object, by default flexjson will not serialize the collections in a bean

out.contentType

application/json

http reponse content type

out.statusCode

200

http status code of the response, sometimes 500 maybe used to indicate an exception

out.characterEncoding

utf-8

http response character encoding

out.gzip

false

whether to use gzip to compress the response, to reduce the network transfer load

out.noCache

false

when set to true, the following 3 headers will be set. Cache-Control set to no-cache, Expires set to 0 and Pragma set to No-cache

prefix

false

when set to true, a prefix "{}&&" would be added before the json result, this is to prevent json attack, see this blog for the detail

callbackParameter

null

when set, the result will be wrapped with a function. The function name is speicified by this callbackParameter. For example: when callbackParameter is set to callback,

http://localhost:9080/spiderx/test/test?callback=alert

will generate a result like

Code Block

Exception Handling

You can define some global exception mapping in your struts.xml. when exception is thrown, an special status code can be used, and the client can deal with different exception type by the status code.

For example:

...

...

Now when an exception occurs, for example a NullPointerException, following json will be the output with a status code 500:

...

...

Notice the patterns for flexjson:

...

Version

Date

Author

Notes

0.99

Mar 6, 2009

Li Chang Geng Image Added

Initial release candidate