Name |
JSON Plugin |
---|---|
Author |
|
Homepage |
|
Version |
0.1-BETA |
Compatibility |
Struts 2.0.2+ |
State |
Unstable |
License |
Open Source (ASL2) |
Download |
Overview
This plugin provides a "json" result type that serializes actions into JSON. Any field that has a "getter" method and is not 'transient' will be serialized. The serialization process is recursive, meaning that the whole object graph, starting on the action class (base class not included) will be serialized.
Example
Setup Action
This simple action has some fields:
Example:
import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.Action; public class JSONExample { private String field1 = "str"; private int[] ints = {10, 20}; private Map map = new HashMap(); //'transient' fields are not serialized private transient String field2; //fields without getter method are not serialized private String field3; public String execute() { map.put("John", "Galt"); return Action.SUCCESS; } public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } public int[] getInts() { return ints; } public void setInts(int[] ints) { this.ints = ints; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } }
Write the mapping for the action
- Add the map inside a package that extends "json-default"
- Add a result of type "json"
Example:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="example" extends="json-default"> <action name="JSONExample" class="example.JSONExample"> <result type="json"/> </action> </package> </struts>
JSON example output
{ "field1" : "str", "ints": [10, 20], "map": { "John":"Galt" } }
Installation
This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib
directory. No other files need to be copied or created.
Version History
Version |
Date |
Author |
Notes |
---|---|---|---|
0.1-BETA |
Jan 11, 2007 |
Initial release |