Reputation: 57
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 its 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 struts2-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, that the 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"}]
}
Upvotes: -1
Views: 1108
Reputation: 1
Because you are using Struts2. Struts2 is used to capitalize property name to resolve a getter method, before a "get" method name could be resolved a property "qRes"
capitalized to "QRes"
and prefixed with "get". So, the method name will be getQRes
. You can remove root
parameter from the result and see if it can be used otherwise, or rename a getter method name as
public QueryResponse getQRes() {
return qRes;
}
You can also read this question about why generated getters and setters in Eclipse don't work with Struts2 OGNL.
Upvotes: 1
Reputation: 1821
I think you are not reading data properly at client side. Try 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(t);
console.log(d[0].id);
console.log(d[0].name);
}
});
Upvotes: 0