【问题标题】:Why can't I get json from struts 2 action?为什么我不能从 struts 2 动作中获取 json?
【发布时间】:2015-04-13 08:23:34
【问题描述】:

我想使用 jquery 向我的 struts 操作发送 ajax 发布请求,如下所示:

$.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);
        }
    });

我的动作配置:

<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>

我的动作课:

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 和 QueryResponse 只是 java bean,当我执行我的代码时,服务器可以读取 json 数据并正确完成它的工作,我确信 QueryResponse 对象填充了数据。但我可以'在客户端什么都得不到,responseText 和 responseJson 都是空的。

为什么 struts-json 插件没有自动将我的 QueryResponse 对象转换为 json 字符串?

我的 QueryResponse 类:

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;
    }


}

数据列表包含的实际类型必须在运行时确定,例如,如果数据包含角色对象列表: 包 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;
    }

}

那么预期的结果是这样的:

{
total : "1", data :[{"id":"admin", "name":"admin role"}]
}

【问题讨论】:

  • 您只使用了一个拦截器来执行您的操作。使用默认堆栈 + json 拦截器。
  • 你在使用 Struts 2 JSON 插件吗?

标签: jquery struts2 ognl struts2-json-plugin


【解决方案1】:

因为您使用的是 Struts2。 Struts2 用于将属性名称大写以解析 getter 方法,在解析“get”方法名称之前,可以将属性 "qRes" 大写为 "QRes" 并以“get”为前缀。因此,方法名称将是getQRes。您可以从结果中删除root 参数,看看是否可以以其他方式使用它,或者将getter 方法名称重命名为

public QueryResponse getQRes() {
    return qRes;
}

您还可以阅读this 问题,了解为什么 Eclipse 中生成的 getter 和 setter 不适用于 Struts2 OGNL。

【讨论】:

  • 我现在正在使用 Gson 将对象序列化为 json。不过我稍后会根据您所说的进行测试,谢谢。
【解决方案2】:

我认为您没有在客户端正确读取数据。试试这个:

$.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);
    }
});

【讨论】:

    猜你喜欢
    • 2015-08-12
    • 2017-05-02
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2017-05-20
    • 2020-08-31
    • 1970-01-01
    相关资源
    最近更新 更多