1、问题描述
在使用 Struts2 的 Action 实现一些 Ajax 接口的过程中,在调用接口时,总是返回一个空的json对象,即{},事实上应该返回{"code":500,"msg":{"msg":"正文不能为空"}}。
2、原因分析
通过调试发现,是访问权限控制符引起的。使用 protected 时,外界不能通过 getter 方法获取到属性的值,所以必须使用允许外界访问的公开权限控制符,即public。
产生问题代码如下:
protected int code;
protected Map msg = new HashMap();
protected int getCode(){return code;}
protected Map getMsg(){return msg;}
3、解决方法
通过以上分析,我们只需要将访问权限控制符从protected改成public,就可以解决这个问题。
具体代码如下:
public int code;
public Map msg = new HashMap();
public int getCode(){return code;}
public Map getMsg(){return msg;}
var duoshuoQuery = {short_name:"yangchenglong"};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();