执行struts 2动作类时给出空的json结果 [英] Giving an empty json result while executing struts 2 action class

查看:126
本文介绍了执行struts 2动作类时给出空的json结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Pojo class :

public class TiendayaCurrencies{


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

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

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

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"})})

...

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

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

推荐答案

Struts2 JSON插件将序列化您的整个操作,包括使用吸气剂的所有(非瞬态)属性.

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

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

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+\\]\\..*"})
})

还请注意,您可以指定一个根对象,该对象通常是includeProperties技术的首选,如此处所述:

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"})
})

这篇关于执行struts 2动作类时给出空的json结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆