2019独角兽企业重金招聘Python工程师标准>>>
测试struts.xml中result参数的不同返回不同的json数据
目的是为了比较result中type不同和result中参数的不同所产生的效果
如果查询的是所有的数据,在action中定义的类型如下:
private List<Goods> entities;
public List<Goods> getEntities() {
return entities;
}
1.第一种:
在xml文件中如果没有param,内容如下:
<action name="goods" class="cn.csdn.hr.action.GoodsAction">
<result type="json"/>
</action>
它得到的是json数据,但是autocomplete解析不出这样的json格式,所以读取不出来,结果为:
{"entities":[{"id":2,"name":"老王 ","price":222.0},{"id":1,"name":"老王 ","price":222.0},{"id":3,"name":"老王 ","price":222.0},{"id":4,"name":"老王 ","price":222.0},{"id":5,"name":"老王 ","price":222.0}]
.
2.第二种:如果加上param的name为多的那个对象,则可以返回的是json数据
entitiesparam>
</result>
action>
返回的数据为:
[{"id":2,"name":"老王 ","price":222.0},{"id":1,"name":"老王 ","price":222.0},{"id":3,"name":"老王 ","price":222.0},{"id":4,"name":"老王 ","price":222.0},{"id":5,"name":"老王 ","price":222.0}]
可见返回的是json数据,并且在智能提示中可以提取出来,可以得知:
<param name="root">entities</param>
的意思是从根节点为entities的开始遍历,所有看到的数据就是json数据了。
3.如果在action中封装的是一个实体,也就是查询的是一条数据,则代码为:
private Goods entity;
public Goods getEntity() {
return entity;
}
响应的数据为:
{"entity":{"id":1,"name":"老王 2","price":222.0}}
4.如果加上param参数,则返回的是
entityparam>
</result>
action>
结果为:{"id":1,"name":"老王 2","price":222.0}
5.如果在action中的数据为:
private List<Goods> entities;
private Goods entity;
public List<Goods> getEntities() {
return entities;
}
public Goods getEntity() {
return entity;
}
在xml文件中的数据为:
<action name="goods" class="cn.csdn.hr.action.GoodsAction">
<result type="json">
<param name="root">entity</param>
result>
</action>
则返回的只是entity对应的实体
{"id":1,"name":"老王 2","price":222.0}
6.如果为:
entitiesparam>
</result>
action>
则返回的只是entities的实体,为
[{"id":2,"name":"老王 ","price":222.0},{"id":1,"name":"老王 ","price":222.0},{"id":3,"name":"老王 ","price":222.0},{"id":4,"name":"老王 ","price":222.0},{"id":5,"name":"老王 ","price":222.0}]
7.如果在xml中的文件为:
<action name="goods" class="cn.csdn.hr.action.GoodsAction">
<result type="json">
<param name="root">entities,entity</param>
result>
</action>
那么返回的数据为:
{"id":1,"name":"老王 2","price":222.0}
也就是谁在后面返回的是哪个
.8。如果xml数据为:
entitiesparam>
</result>
返回的结果为:
{"entities":[]}
9.如果xml中数据为:
entities\[\d+\].nameparam>
</result>
返回的数据为:
{"entities":[{"name":"老王1 "},{"name":"老王 2"},{"name":"老王3 "},{"name":"老王4 "},{"name":"老王5 "}]}
10.如果xml中数据为:
^entities\[\d+\].nameparam>
result>
返回的数据为:
{"entities":[{"name":"老王1 "},{"name":"老王 2"},{"name":"老王3 "},{"name":"老王4 "},{"name":"老王5 "}]}
这个是返回全部Action中getter方法的参数action
还有就是官网的介绍,听详细的!
http://struts.apache.org/release/2.2.x/docs/json-plugin.html
The JSON plugin provides a "json" result type that serializes actions into JSON. The serialization process is recursive, meaning that the whole object graph, starting on the action class (base class not included) will be serialized (root object can be customized using the "root" attribute). If the interceptor is used, the action will be populated from the JSON content in the request, these are the rules of the interceptor:
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:
{ "doubleValue": 10.10, "nestedBean": { "name": "Mr Bean"
}, "list": ["A", 10, 20.20, { "firstName": "El Zorro"
}], "array": [10, 20]
}
The action must have a "setDoubleValue" method, taking either a "float" or a "double" argument (the interceptor will convert the value to the right one). There must be a "setNestedBean" whose argument type can be any class, that has a "setName" method taking as argument an "String". There must be a "setList" method that takes a "List" as argument, that list will contain: "A" (String), 10 (Long), 20.20 (Double), Map ("firstName" -> "El Zorro"). The "setArray" method can take as parameter either a "List", or any numeric array.
So serialize your objects to JSON in javascript see json2 |
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.
To use maven, add this to your pom:
...
org.apache.struts
struts2-json-plugin
STRUTS_VERSION
...
Customizing Serialization and Deserialization
Use the JSON annotation to customize the serialization/deserialization process. Available JSON annotation fields:
Name | Description | Default Value | Serialization | Deserialization |
---|---|---|---|---|
name | Customize field name | empty | yes | no |
serialize | Include in serialization | true | yes | no |
deserialize | Include in deserialization | true | no | yes |
format | Format used to format/parse a Date field | "yyyy-MM-dd'T'HH:mm:ss" | yes | yes |
Excluding properties
A comma-delimited list of regular expressions can be passed to the JSON Result and Interceptor, properties matching any of these regular expressions will be ignored on the serialization process:
login.password,
studentList.*\.sin
true
login.password,
studentList.*\.sin
Including properties
A comma-delimited list of regular expressions can be passed to the JSON Result to restrict which properties will be serialized. ONLY properties matching any of these regular expressions will be included in the serialized output.
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. |
^entries\[\d+\]\.clientNumber,
^entries\[\d+\]\.scheduleNumber,
^entries\[\d+\]\.createUserId
Root Object
Use the "root" attribute(OGNL expression) to specify the root object to be serialized.
person.job
The "root" attribute(OGNL expression) can also be used on the interceptor to specify the object that must be populated, make sure this object is not null.
bean1.bean2
Wrapping
For several reasons you might want to wrap the JSON output with some text, like wrapping with comments, adding a prefix, or to use file uploads which require the result to be wrapped in a textarea. Use wrapPrefix to add content in the beginning and wrapPostfix to add content at the end. This settings take precedence over "wrapWithComments" and "prefix" which are deprecated from 0.34 on. Examples:
Wrap with comments:
/*
*/
Add a prefix:
{}&&
Wrap for file upload: