I want to send ajax post request to my struts action using jquery like this:
$.ajax({
type: "POST",
url: "ds/query",
data :JSON.stringify(data),
dataType:"json",
contentType: "application/json; charset=utf-8",
success : function(d, t, x){
console.log(x);
}
});
my action configuaration:
<package name="ds" namespace="/ds" extends="json-default">
<action name="query" class="gov.cbrc.gzbanking.action.DataServiceAction" method="query">
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
<result type="json">
<param name="noCache">true</param>
<param name="excludeNullProperties">true</param>
<param name="root">qRes</param>
</result>
</action>
</package>
my action class :
public class DataServiceAction {
private QueryRequest qReq;
private QueryResponse qRes;
public QueryRequest getQr() {
return qReq;
}
public void setQr(QueryRequest qReq) {
this.qReq = qReq;
}
public QueryResponse getqRes() {
return qRes;
}
public void setqRes(QueryResponse qRes) {
this.qRes = qRes;
}
public String query() throws Exception{
App.getLogger().debug(qReq.toString());
String dsClz = SysCodes.getCodeItem("data_service", qReq.getDs()).getConfig1();
Class<?> dsCls = Class.forName(dsClz);
if(!DataService.class.isAssignableFrom(dsCls)){
throw new Exception("specified class does't implement DataService interface.");
}else{
DataService ds = (DataService) dsCls.newInstance();
System.out.println(JsonUtil.toJson(ds.query(qReq)));
qRes = ds.query(qReq);
}
return ActionSupport.SUCCESS;
}
}
QueryRequest and QueryResponse are just java beans, when I execute my code, Server can read json data and do it's job without error, and I am sure QueryResponse object is popluted with data. but I can't get anything at client side, responseText and responseJson are both null.
why did not struts-json plugin convert my QueryResponse object into json string automaticaly?
my QueryResponse class :
public class QueryResponse {
private int total;
private List<?> data;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}
}
the actual type of data list contains must be determined at runtime, for example, if data contains list of Role objects: package gov.cbrc.gzbanking.domain;
public class Role {
private String id;
private String name;
private String remark;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
then expected result is like this:
{
total : "1", data :[{"id":"admin", "name":"admin role"}]
}