【发布时间】:2015-06-28 11:16:09
【问题描述】:
我想从服务器获取 json 响应,因为我正在使用以下代码
public class AtomAddressDetail implements java.io.Serializable {
private Long id;
private Place placeByStateId;
private Atom atom;
private Place placeByCountryId;
private Place placeByCityId;
private Place placeByStreetId;
private Place placeByAreaId;
private String houseno;
//getter and setter
}
public class Place implements java.io.Serializable {
private Long id;
private String name;
private String about;
//getter and setter
}
在行动
public class SettingAction extends ActionSupport {
private long pageId; //getter and setter
private long id; //getter and setter
private List<AtomAddressDetail> atomAddressList;
public String singleAddress() {
setAtomAddressList(cdao.singleAddress(getId(), getPageId()));
for (AtomAddressDetail a : getAtomAddressList()) {
System.out.println("Country " + a.getPlaceByCountryId().getId() + " " + a.getPlaceByCountryId().getName());
System.out.println("state " + a.getPlaceByStateId().getId() + " " + a.getPlaceByStateId().getName());
System.out.println("city " + a.getPlaceByCityId().getId() + " " + a.getPlaceByCityId().getName());
System.out.println("area " + a.getPlaceByAreaId().getId() + " " + a.getPlaceByAreaId().getName());
System.out.println("street " + a.getPlaceByStreetId().getId() + " " + a.getPlaceByStreetId().getName());
}
}
public List<AtomAddressDetail> getAtomAddressList() {
return atomAddressList;
}
public void setAtomAddressList(List<AtomAddressDetail> atomAddressList) {
this.atomAddressList = atomAddressList;
}
}
输出:
Country 2 India
state 3 asdf
city 4 sdfsd
area 5 www
street 6 sdfdsa f
在struts.xml
<action name="SingleAddressDetail" class=".SettingAction" method="singleAddress">
<result name="success" type="json">
<param name="includeProperties">
^atomAddressList\[\d+\]\.id,
^atomAddressList\[\d+\]\.houseno,
^atomAddressList\[\d+\]\.placeByAreaId.id,
^atomAddressList\[\d+\]\.placeByAreaId.name,
^atomAddressList\[\d+\]\.placeByCityId.id,
^atomAddressList\[\d+\]\.placeByCityId.name,
^atomAddressList\[\d+\]\.placeByStateId.id,
^atomAddressList\[\d+\]\.placeByStateId.name,
^atomAddressList\[\d+\]\.placeByCountryId.id,
^atomAddressList\[\d+\]\.placeByCountryId.name
</param>
<param name="excludeNullProperties">true</param>
<param name="root">
#action
</param>
</result>
<result name="input" type="json"/>
<result name="login" type="json"></result>
</action>
在 JSP 中
{"atomAddressList":[{"houseno":"sadf sadf ","id":1}]}
问题出在 JSP 页面中,我只获得了两个字段值,但我想获取在 struts.xml 的操作中指定的所有值。
如前所述,值已正确打印,但在 JSP 中访问时,如 alert(data.atomAddressList[0].placeByCountryId.id); 会显示
error:Uncaught TypeError: Cannot read property 'id' of undefined
【问题讨论】:
-
你能把
atomAddressList指定为root而不是使用includeProperties参数吗? -
显示
null。 -
尝试使用
<result type="json"/>w/o 参数并发布 json 响应。 -
显示错误
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException -
嗯...你确定没有任何附加参数的
<param name="root">atomAddressList</param>不起作用吗?
标签: java regex json jsp struts2