struts2怎么返回json
求高手指教,为什么不执行,代码有什么问题吗?
struts2配置:
<action name="changeAddress" class="OrderAction" method="changeAddress">
<result name="success" type="json">
</result>
</action>
Action代码:
private int addId;
private ReceiveAddress jsonAddress;
public String changeAddress() {
System.out.println(addId+".....................");
jsonAddress = iorderserv.findReceiveAddressById(addId); //数据库取数据
System.out.println(jsonAddress.getFullAddress()); //打印调试,输出了
//TODO
return "success";
}
public ReceiveAddress getJsonAddress() {
return jsonAddress;
}
public void setJsonAddress(ReceiveAddress jsonAddress) {
this.jsonAddress = jsonAddress;
}
public int getAddId() {
return addId;
}
public void setAddId(int addId) {
this.addId = addId;
}
js代码:
function selectAddress(id){
alert(id); //执行了
$("input[type='text']").val("");
$.post("changeAddress", {"addId":id}, function(msg){
alert(msg.jsonAddress.phone); //为什么这里不执行
$("#receiveName").val(add.receiveName);
$("#fullAddress").val(add.fullAddress);
$("#postalCode").val(add.postalCode);
$("#phone").val(add.phone);
$("#mobile").val(add.mobile);
}, "json");
}
------解决方案--------------------
ACTION:
String jsonArray = JSONArray.toJSONString(listStreet);
------解决方案--------------------
前提是需要 struts2-json-plugin-2.2.1.1.jar
<package name="default" namespace="/index" extends="json-default">
并且将你需要返回的对象设置为成员变量,并生成get,set方法
因为你的成员变量有很多,所有需要在配置文件中设置param参数:
excludeProperties 表示不包含的属性
includeProperties 表示包含的属性