测试struts.xml中result参数的不同返回不同的json数据

测试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数据
<action name="goods" class="cn.csdn.hr.action.GoodsAction">
			<result type="json">
				<param name="root">entities</param>
			</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参数,则返回的是
<action name="goods" class="cn.csdn.hr.action.GoodsAction">
			<result type="json">
				<param name="root">entity</param>
			</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.如果为:
<action name="goods" class="cn.csdn.hr.action.GoodsAction">
			<result type="json">
				<param name="root">entities</param>
			</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数据为:
<result type="json">
		<param name="includeProperties">entities</param>
</result>

返回的结果为:
{"entities":[]}


9.如果xml中数据为:
<result type="json">
				<param name="includeProperties">entities\[\d+\].name</param>
			</result>

返回的数据为:
{"entities":[{"name":"老王1 "},{"name":"老王 2"},{"name":"老王3 "},{"name":"老王4 "},{"name":"老王5 "}]}


10.如果xml中数据为:
<result type="json">
				<param name="includeProperties">^entities\[\d+\].name</param>
</result>
返回的数据为:
{"entities":[{"name":"老王1 "},{"name":"老王 2"},{"name":"老王3 "},{"name":"老王4 "},{"name":"老王5 "}]}


你可能感兴趣的:(json,xml,autocomplete,测试,Class,action)