I want to fetch json response from server for that I am using following code
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
}
In action
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;
}
}
Output:
Country 2 India
state 3 asdf
city 4 sdfsd
area 5 www
street 6 sdfdsa f
In 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>
In JSP
{"atomAddressList":[{"houseno":"sadf sadf ","id":1}]}
Problem is in JSP page I am getting only two filds value but I want to fetch all values that are specified in struts.xml
's action.
Values are printed in action properly as mentioned but on accessing in JSP like alert(data.atomAddressList[0].placeByCountryId.id);
it is showing
error:Uncaught TypeError: Cannot read property 'id' of undefined
You haven't included some properties in the json result because they should be valid regex expressions for each property. A period character should be escaped.
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.