在执行struts 2动作类时给出一个空的json结果

我正在尝试使用休眠 ORM 从数据库中检索数据,并使用 Struts2 将输出作为 json 结果.一切都可以从数据库中检索数据,但对于 json 结果,我只得到 {}.

我认为我的编码有问题.但是需要一些帮助才能弄清楚.

<块引用>

这是我的动作类:

@ParentPackage("json-default")公共类 SocialIconsAction 扩展 ActionSupport {私人列表<TiendayaCurrencies>_货币;公开列表<TiendayaCurrencies>获取货币(){返回_货币;}public void setCurrency(List<TiendayaCurrencies>_currency) {this._currency = _currency;}@Action(值 = "货币", 结果 = {@Result(name = "success", type = "json", params = {"includeProperties","_currency\[\d+\]\..*"})})@覆盖公共字符串执行(){_currency = loadCurrencies();/*数据库结果没有问题.只是为了测试一切正常.*///for (TiendayaCurrencies _currency1 : _currency) {//System.out.println("Title - "+_currency1.getTitle());//}返回成功;}私人列表<TiendayaCurrencies>加载货币(){会话会话 = com.tiendaya.connection.HibernateUtil.getSessionFactory().openSession();列出<TiendayaCurrencies>cList = 会话.createCriteria(TiendayaCurrencies.class).list();返回 cList;}}

<块引用>

Pojo 类:

公共课 TiendayaCurrencies{私人整数 id;私有字符串标题;私有字符串代码;私有字符串符号左;私有字符串符号右;私人字符小数点;...

includeProperties 有什么问题吗?(只有我能想到的地方..)任何人都可以提出一种方法.. 我已经尝试了所有方法...

<块引用>

公共类 SocialIconsAction 扩展 ActionSupport {私人列表<TiendayaCurrencies>_currency=new ArrayList<>();私人字符串样本=工作";公共字符串 getSample() {返回样品;}公共无效setSample(字符串样本){this.sample = 样本;}...@Action(值 = "货币", 结果 = {@Result(name = "success", type = "json", params = {"includeProperties", "sample"})})...

作为 json 输出,它给了我: {"sample":"working"} 这意味着它工作正常.那么为什么它不能与 ArrayList 一起使用??

解决方案

Struts2 JSON 插件将序列化您的整个操作,包括所有(非瞬态)属性带有 getter.

由于您隐藏了变量(绝对不是最佳实践,尤其是因为它迫使您手动编写每个 getter 和 setter...brr),并且变量和 getter 的名称不同,所以您指向变量,但你应该指向 getter(然后是 currency 而不是 _currency):

@Action(value = "currencies", results = {@Result(name = "成功",类型=json",参数 = {"includeProperties","currency\[\d+\]\..*"})})

还请注意,您可以指定一个根对象,这通常优于 includeProperties 技术,如 此处描述:

@Action(value = "currencies", results = {@Result(name = "成功",类型=json",参数 = {"root","货币"})})

Im trying to retrieve data from DB using hibernate ORM and get the out-put as json result using Struts2. Everything work up to retrieving data from DB, but for the json result I get only {}.

I think I have done something wrong with my coding. But need some help to figure it out.

Here is my Action class :

@ParentPackage("json-default")
public class SocialIconsAction extends ActionSupport {

    private List<TiendayaCurrencies> _currency;

    public List<TiendayaCurrencies> getCurrency() {
        return _currency;
    }

    public void setCurrency(List<TiendayaCurrencies> _currency) {
        this._currency = _currency;
    }

    @Action(value = "currencies", results = {
        @Result(name = "success", type = "json", params = {"includeProperties",
            "_currency\[\d+\]\..*"})})
    @Override
    public String execute() {
        _currency = loadCurrencies();

        /*Nothing wrong with the DB results.Just to  test everything works fine.*/
        //for (TiendayaCurrencies _currency1 : _currency) {
           // System.out.println("Title - "+_currency1.getTitle());
       // }


        return SUCCESS;
    }

    private List<TiendayaCurrencies> loadCurrencies() {
        Session session = com.tiendaya.connection.HibernateUtil.
                getSessionFactory().openSession();
        List<TiendayaCurrencies> cList = session.
                createCriteria(TiendayaCurrencies.class).list();

        return cList;
    }
}

Pojo class :

public class TiendayaCurrencies{


     private Integer id;
     private String title;
     private String code;
     private String symbolLeft;
     private String symbolRight;
     private char decimalPlace;
     ...

Is there anything wrong with the includeProperties?(Only place I can think of..) Can any one suggest a way.. I 've tried everything...

Edit :

public class SocialIconsAction extends ActionSupport {

    private List<TiendayaCurrencies> _currency=new ArrayList<>();
    private String sample="working";

    public String getSample() {
        return sample;
    }

    public void setSample(String sample) {
        this.sample = sample;
    }
    ...


@Action(value = "currencies", results = {
@Result(name = "success", type = "json", params = {"includeProperties", "sample"})})

...

As json output it gives me : {"sample":"working"} which means it works fine. So why it is not working with the ArrayList??

解决方案

Struts2 JSON plugin will serialize your whole action, including all the (non-transient) properties with a getter.

Since you are hiding your variables (definitely not a best practice, especially because it forces you to manually write every getter and setter... brr), and you have different names for the variable and for the getter, you are pointing the variable, but you should point the getter (then currency instead of _currency):

@Action(value = "currencies", results = {
    @Result(name = "success", 
            type = "json", 
          params = {"includeProperties","currency\[\d+\]\..*"})
})

Also note that you can specify a root object, that is often preferred to the includeProperties technique, as described here:

@Action(value = "currencies", results = {
    @Result(name = "success", 
            type = "json", 
          params = {"root","currency"})
})

相关文章