关闭

Struts2的json数据的访问

732人阅读 评论(0) 收藏 举报
action中:
	private  List<Admins> entities;
	private Admins admin;
	public Admins getAdmin() {
		return admin;
	}
	public List<Admins> getEntities() {
		return entities;
	}
	public String  execute(){
		AdminsService asi = new AdminsServiceImpl();
		entities = asi.getObjects(Admins.class);
		admin  = (Admins) asi.findObjectById(Admins.class, 1);
	}

配置文件中的内容为
形式1:
<result type="json"> 
	<param name="root"></param> 
</result>
测试的结果为:
{"admin":
{"address":null,"cellPhone":null,"email":null,"id":1,"password":"ssssssss1","realName":"sss1","role":null,"sex":null,"userName":"redarmy1"},
"entities":
[{"address":null,"cellPhone":null,"email":null,"id":1,"password":"ssssssss1","realName":"sss1","role":null,"sex":null,"userName":"redarmy1"},
{"address":null,"cellPhone":null,"email":null,"id":2,"password":"ssssssss2","realName":"sss2","role":null,"sex":null,"userName":"redarmy2"}, {"address":null,"cellPhone":null,"email":null,"id":6,"password":"ssssssss6","realName":"sss6","role":null,"sex":null,"userName":"redarmy6"},
{"address":null,"cellPhone":null,"email":null,"id":7,"password":"ssssssss7","realName":"sss7","role":null,"sex":null,"userName":"redarmy7"},
{"address":null,"cellPhone":null,"email":null,"id":8,"password":"ssssssss8","realName":"sss8","role":null,"sex":null,"userName":"redarmy8"},
{"address":null,"cellPhone":null,"email":null,"id":9,"password":"ssssssss9","realName":"sss9","role":null,"sex":null,"userName":"redarmy9"},
{"address":null,"cellPhone":null,"email":null,"id":3,"password":"ssssssss3","realName":"sss3","role":null,"sex":null,"userName":"redarmy3"},
{"address":null,"cellPhone":null,"email":null,"id":4,"password":"ssssssss4","realName":"sss4","role":null,"sex":null,"userName":"redarmy4"},
{"address":null,"cellPhone":null,"email":null,"id":5,"password":"ssssssss5","realName":"sss5","role":null,"sex":null,"userName":"redarmy5"}]}
因为前面返回的是一个admins对象还有一个集合对象,而且又没有设置要按哪个根节点元素去遍历。所以会把所有的信息都显示出来,返回的不是真正的json数据

形式2:
以entities为根去遍历,所有返回的是真正的json数据,会显示出自动提示效果
<result type="json"> 
	<param name="root">entities</param> 
</result>

形式3:
<result type="json">
	<param name="root">entities,admin</param>
</result>
响应为
{"address":null,"cellPhone":null,"email":null,"id":1,"password":"ssssssss1","realName":"sss1","role":null,"sex":null,"userName":"redarmy1"}
因为设置了两个根节点,所有第一个根节点会被第二个根节点顶替,会按照第二个参数去设置,但返回的不是真正的json数据,所有不会出现自动提示功能

形式4:
<result type="json">
	<param name="includeProperties">entities</param>
</result> 
响应为:
{"entities":[]}
includeProperties会包含参数作为属性,所以查不到内容

形式5:
<result type="json">
	<param name="includeProperties">entities\[\d+\]\.realName</param>
</result> 
响应为:
{"entities":[{"realName":"sss1"},{"realName":"sss2"},{"realName":"sss6"},{"realName":"sss7"},{"realName":"sss8"},
{"realName":"sss9"},{"realName":"sss3"},{"realName":"sss4"},{"realName":"sss5"}]}
includeProperties是包含参数作为作为属性,entities\[\d+\]\.realName是以entities作为根节点去遍历下面的realName,返回的不是真正的json数据

形式6:
<result type="json">
	<param name="includeProperties">^entities\[\d+\]\.realName</param>
</result>
响应为:
{"entities":[{"realName":"sss1"},{"realName":"sss2"},{"realName":"sss6"},{"realName":"sss7"},{"realName":"sss8"},
{"realName":"sss9"},{"realName":"sss3"},{"realName":"sss4"},{"realName":"sss5"}]}
^:匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
原因同上,不过由于list集合的根节点本来就是entities,所有全部匹配

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:403495次
    • 积分:5938
    • 等级:
    • 排名:第4428名
    • 原创:210篇
    • 转载:15篇
    • 译文:0篇
    • 评论:38条
    文章分类
    最新评论