首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Apache Struts 二 Documentation-JSON Plugin

2012-08-21 
Apache Struts 2 Documentation--JSON PluginThe content-type must be application/json The JSON co

Apache Struts 2 Documentation--JSON Plugin

    The "content-type" must be "application/json" The JSON content must be well formed, see json.org for grammar. Action must have a public "setter" method for fields that must be populated. Supported types for population are: Primitives (int,long...String), Date, List, Map, Primitive Arrays, Other class (more on this later), and Array of Other class. Any object in JSON, that is to be populated inside a list, or a map, will be of type Map (mapping from properties to values), any whole number will be of type Long, any decimal number will be of type Double, and any array of type List.

Given this JSON string:

So serialize your objects to JSON in javascript see json2Installation

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.

To use maven, add this to your pom:

Note
Exclude property expressions take precedence over include property expressions. That is, if you use include and exclude property expressions on the same result, include property expressions will not be applied if an exclude exclude property expression matches a property first.make sure this object is not null.

Wrap with comments:

wrapWithComments is deprecated from 0.34, use wrapPrefix and wrapSuffix instead.wrapWithComments can turn safe JSON text into dangerous text. For example,

prefix 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 objects

As 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>

热点排行