Struts可以返回为JSON的形式。
[b]文档[/b]
中文文档:http://webservices.ctocio.com.cn/tips/424/7670924.shtml
英文文档:http://cwiki.apache.org/S2PLUGINS/json-plugin.html(详细些)
[b]有几个需要注意的地方:[/b]
1: 如果不序列化某个属性需要在get方法上加入@JSON(deserialize=false,serialize=false)注释。
2: struts2.0包必须继承json-default,<package name="default" extends="json-default">
3:struts设置为UTF-8 , <constant name="struts.i18n.encoding" value="UTF-8" />
4: Model的访问控制符必须是public的,public static class Model
5:设置返回类型为:json
<action name="knowledgeAction" class="KnowledgeAction">
<result type="json" >
</result>
</action>
输入的JAVA对象:
输出的JSON对象
[b]文档[/b]
中文文档:http://webservices.ctocio.com.cn/tips/424/7670924.shtml
英文文档:http://cwiki.apache.org/S2PLUGINS/json-plugin.html(详细些)
[b]有几个需要注意的地方:[/b]
1: 如果不序列化某个属性需要在get方法上加入@JSON(deserialize=false,serialize=false)注释。
2: struts2.0包必须继承json-default,<package name="default" extends="json-default">
3:struts设置为UTF-8 , <constant name="struts.i18n.encoding" value="UTF-8" />
4: Model的访问控制符必须是public的,public static class Model
5:设置返回类型为:json
<action name="knowledgeAction" class="KnowledgeAction">
<result type="json" >
</result>
</action>
输入的JAVA对象:
List<Knowledge> knowledges = new ArrayList<Knowledge>();
//创建对象
Knowledge knowledge = new Knowledge();
knowledge.setInfoId("know1000");
knowledge.setInfoTitle("infoTitle");
knowledge.setCreatorId("creatorId1000");
knowledge.setCreatorName("creatorName");
knowledge.setCreateTime(new Date());
knowledge.setAuditorId("auditorId1000");
knowledge.setAuditorName("auditorName");
knowledge.setAuditTime(new Date());
knowledge.setStatus(300.2512);
knowledge.setInfoType("infoType");
knowledge.setCategoryId("categoryId1000");
knowledge.setReadCount(29.23);
knowledge.setInfoLevel(12.3);
knowledge.setModifierId("modifierId1000");
knowledge.setModifierName("modifierName");
knowledge.setModifyTime(new Date());
knowledges.add(knowledge);
输出的JSON对象
{"model":{"knowledges":
[
{"auditTime":"2008-10-20 17:52:48",
"auditorId":"auditorId1000",
"auditorName":"auditorName",
"categoryId":"categoryId1000",
"createTime":"2008-10-20 17:52:48","creatorId":"creatorId1000",
"creatorName":"creatorName",
"infoId":"know1000",
"infoLevel":12.3,
"infoTitle":"infoTitle",
"infoType":"infoType","modifierId":"modifierId1000","modifyTime":"2008-10-20 17:52:48","readCount":29.23,"status":300.2512}]}}