从Struts2 Action返回JSON对象时未返回任何数据

3

实际应用:

//Declaration
    JSONObject jObj1 = null;

    public JSONObject getjObj1() {
            return jObj1;
   }
   public void setjObj1(JSONObject jObj1) {
            this.jObj1 = jObj1;
   }

 //In Action method   
String jsong="{\"cid\":232}";
jObj1 = new JSONObject(jsong);

return Action.SUCCESS

Struts配置文件

<action name="jsonAction" class="jsonAction" method="getJson">
            <result type="json" name="success">
                <param name="root">jObj1</param>                
            </result> 
        </action>

当我在JSP控制台中查看时,结果为空。

我做错了什么?谢谢。


为什么你不使用S2提供的JSON插件? - Umesh Awasthi
可能是重复的问题:jQuery AJAX - issue returning JSON value - Roman C
1个回答

2
你把它复杂化了 :) Struts2-JSON-Plugin会将你的Action对象序列化为JSON,因此你不需要(也是错误的)自己对它们进行编码。
然后,保持你的代码和配置不变,只需更改Action为:
//Declaration
Map<String,Integer> jObj1 = null;

/* Getter and Setter */

//In Action method   
jObj1 = new HashMap<String,Integer>();
jObj1.put("cid",232);
return Action.SUCCESS

@Ligios,感谢您的回复。实际情况是,我从Web服务获取JSON作为字符串(DTO对象列表),我想将其返回到JSP页面。我该怎么做? - sasi
使用json拦截器和流结果,在我的答案链接中,Roman C的答案中有。 - Andrea Ligios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接