How to override the CRUD/list() function? Play! framework

China☆狼群 提交于 2019-12-10 11:19:46

问题


I'm trying to override the list() function of the CRUD module for one of my models.

I found this on google groups which is essentially the issue i'm having.

Basically I want to filter the list based on certian categories, I tried this:

CONTROLLER

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

MODEL

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;
}

I get the following error:

How do you overwrite the list action?

Any help will be much appreciated.


回答1:


It seems that you are overriding the controller but not the template. The signature of the CRUD list method is this one, slightly different that yours:

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

You will notice that render() is passing many more parameters that you do, and probably they are not optional. Try to provide values for them.




回答2:


You can override the CRUD list method, and add filters passing many parameters in where for example:

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:


Try to call that override method from view(xtml).

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

And use previous code,and add filters passing many parameters in where = "..."



来源:https://stackoverflow.com/questions/7827427/how-to-override-the-crud-list-function-play-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!