【问题标题】:Giving an empty json result while executing struts 2 action class在执行struts 2动作类时给出一个空的json结果
【发布时间】:2016-02-01 21:15:36
【问题描述】:

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

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

这是我的动作类:

@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 类:

public class TiendayaCurrencies{


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

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

编辑:

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 一起使用??

【问题讨论】:

  • localhost:8080/Tiendaya_Project_Final/currencies-->(动作值)我如何执行我的动作类。它给了我空的 json 结果。 {}-仅限
  • 你不是在哪里使用那个 JSON 结果吗? .. 你怎么能说 JSON 是空的?我怀疑..在您的 JSP 或某处您指的是错误的 Json List ..
  • 在获得 JSP 的结果之前,我只检查从服务器获取的 json 对象。正如我上面提到的,它是空的。因此,即使我将其设置为 jsp,它也应该给我一个空的 html 元素。(例如:考虑将其设置为选择框)
  • _currency\\[\\d+\\]\\..* 你想用这个做什么?
  • _currency\\[\\d+\\]\\.* 您是否尝试过使用单点. 不需要使用includeProperties 实际上它会默认序列化所有正在运行的属性

标签: java json struts2 struts2-json-plugin struts-action


【解决方案1】:

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

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

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

还请注意,您可以指定一个根对象,这通常比includeProperties 技术更受欢迎,如described here

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

【讨论】:

  • 非常感谢你......你节省了我的另一天......我浪费了很多时间,寻找这个愚蠢的 _currency 单词错误的解决方案。再次感谢您的回答。如果可以的话,我会再次投票..
猜你喜欢
  • 2012-06-04
  • 2017-09-21
  • 2014-10-24
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多