Apache Struts 2 Documentation--JSON Plugin
Given this JSON string:
So serialize your objects to JSON in javascript see json2InstallationThis 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.
To use maven, add this to your pom:
Noteprefix is deprecated from 0.34, use wrapPrefix and wrapSuffix instead.
If the parameter prefix is set to true, the generated JSON will be prefixed with "{}&& ". This will help prevent hijacking. See this Dojo Ticket for details:
Add a result of type "json"Example:
The bean class:
Now the javascript code:
+ bean.price); }; Proxied objectsAs annotations are not inherited in Java, some user might experience problems while trying to serialize objects that are proxied. eg. when you have attached AOP interceptors to your action.
In this situation, the plugin will not detect the annotations on methods in your action.
To overcome this, set the "ignoreInterfaces" result parameter to false (true by default) to request that the plugin inspects all interfaces and superclasses of the action for annotations on the action's methods.
NOTE: This parameter should only be set to false if your action could be a proxy as there is a performance cost caused by recursion through the interfaces.
<action name="contact" class="package.ContactAction" method="smd"> <interceptor-ref name="json"> <param name="enableSMD">true</param> <param name="ignoreSMDMethodInterfaces">false</param> </interceptor-ref> <result type="json"> <param name="enableSMD">true</param> <param name="ignoreInterfaces">false</param> </result> <interceptor-ref name="default"/></action>