【问题标题】:How to override the CRUD/list() function? Play! framework如何覆盖 CRUD/list() 函数?玩!框架
【发布时间】:2013-01-05 04:06:15
【问题描述】:

我正在尝试为我的一个模型覆盖 CRUD 模块的 list() 函数。

我在 google 群组上找到了this,这基本上是我遇到的问题。

基本上我想根据某些类别过滤列表,我试过这个:

控制器

public static void list(string category){
    List<Duty> object = Duty.getByCategory(category);
    render(object);
}

型号

public static List<Duty> getByCategory(String category){
    List<Duty> result = Duty.find("select distinct d from Duty d join " +
        "d.category c where c.name = ? order by d.name", category).fetch();
    return result;
}

我收到以下错误:

如何覆盖列表操作?

任何帮助将不胜感激。

【问题讨论】:

    标签: pagination playframework crud


    【解决方案1】:

    您似乎正在覆盖控制器而不是模板。 CRUD list 方法的签名是this one,和你的略有不同:

    public static void list(int page, String search, String searchFields, String orderBy, String order) {
            ObjectType type = ObjectType.get(getControllerClass());
            notFoundIfNull(type);
            if (page < 1) {
                page = 1;
            }
            List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where"));
            Long count = type.count(search, searchFields, (String) request.args.get("where"));
            Long totalCount = type.count(null, null, (String) request.args.get("where"));
            try {
                render(type, objects, count, totalCount, page, orderBy, order);
            } catch (TemplateNotFoundException e) {
                render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
            }
        }
    

    您会注意到,render() 传递的参数比您传递的更多,而且可能它们不是可选的。尝试为他们提供价值。

    【讨论】:

      【解决方案2】:

      您可以覆盖 CRUD 列表方法,并在 where 中添加传递许多参数的过滤器,例如:

      public static void list(int page, String search, String searchFields, String orderBy, String order) {
          ObjectType type = ObjectType.get(getControllerClass());
          notFoundIfNull(type);
          if (page < 1) {
              page = 1;
          }
          String where = "nameAttribute =" + value;
      
          List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, where);
          Long count = type.count(search, searchFields, where);
          Long totalCount = type.count(null, null, where);
          try {
              render(type, objects, count, totalCount, page, orderBy, order);
          } catch (TemplateNotFoundException e) {
              render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
          }
      }
      

      【讨论】:

        【解决方案3】:

        尝试从view(xtml) 调用该覆盖方法。

        <form action="@{Controler.overrideList()}"  method="POST">
        

        并使用以前的代码,并在where = "..."中添加传递许多参数的过滤器

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-24
          相关资源
          最近更新 更多