以 Wicket 形式显示搜索结果数量

发布于 2024-12-10 06:08:15 字数 332 浏览 1 评论 0原文

在我当前的 Apache Wicket 项目中,我有一个用于查询数据库并在 ListView 中显示查询结果的搜索表单。搜索输入框与带有结果的 ListView 位于同一页面,并且在调用 onSubmit() 方法期间,ListView 填充了来自 DAO 的查询结果的形式。

一切正常,但我需要显示搜索结果的数量。我尝试创建一个 Label,其中填充了通过 ListView 实例的 getList() 方法获取的列表的 size() 方法的值,但没有成功。

感谢您提前提供的任何帮助。

In my current Apache Wicket project I have a search form for querying the database and displaying the query results in a ListView. The search input box is on the same page as the ListView with the results, and that ListView is filled with query results from a DAO, during invocation of the onSubmit() method of the form.

Everything works fine, but I need to display the number of search results. I tried to create a Label that is filled with the value of the size() method of the list got by the getList() method of the ListView instance, but no luck.

Thank you for any help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-12-17 06:08:15

根据您构建此表单的方式,您可能只需要执行 label.setModelObject(listResults.size()) 操作。如果不看看你是如何做的,就很难判断。

根据您在问题中所说的内容,您可能正在创建这样的标签 new Label(labelId, listView.getList().size()。这是行不通的,您在构造时使用常量值设置标签的模型,这就是构造时列表的大小,您需要在 ModelgetObject() 中获取该值。使值“动态”,例如,for。例如,

AbstractReadOnlyModel sizeModel = new AbstractReadOnlyModel(){
    public getObject(){
        return listView.getList().getSize();
    }
}
new Label(labelId, sizeModel);

这样,每次页面呈现时,都会调用 sizeModel().getObejct() 来检索 Label 的值。 code>Label 有一个带有常量值的 Model

您甚至可以在 onSubmit 中执行 label.setModelObject(list.size()) 。 () 方法

。不知道如何构建此表单,我将向您展示如何使用 LoadableDetachableModel 检索结果的 ListListView 的代码>模型。然后,Label 可以有一个 AbstractReadOnlyModel,它使用 ListView 的 modelObject 来获取其大小。

public class MyForm extends Form {


    private LoadableDetachableModel resultsModel;
    private IModel searchModel;
    public MyForm(){
        searchModel = new Model();
        TextField searchTextField = new TextField("search", searchModel);
        resultsModel = new LoadableDetachableModel(){
             protected Object load(){
                 return myService.get(searchModel.getModelObject());
             }
        }
        ListView lv = new ListView("list", resultsModel){
            // ...
        }
        Label resultsCount = new Label("count", new AbstractReadOnlyModel(){
              public Object getObject(){
                   return ((List) resultsModel.getObject()).size();
              }
        })
        SubmitButton button = new SubmitButton(){
             public void onSubmit(){
                 //... No actions needed, really
             }
        }
        // add's...
    }
}

使用 LoadableDetachableModel 对于 ListView 来说,它的优点是自动分离 Model,从而避免整个 List 结果被序列化到会话

Depending on how you have built this form, you might only need to do label.setModelObject(listResults.size()). It's difficult to tell without seeing how are you doing it.

By what you're telling in your question, probably you're creating your Label like this new Label(labelId, listView.getList().size(). This won't work, you're setting the Label's Model at construction time with a constant value, that's the size of the list at construction time. You need to get that value inside a Model's getObject() to make the value "dynamic". Like, for instance,

AbstractReadOnlyModel sizeModel = new AbstractReadOnlyModel(){
    public getObject(){
        return listView.getList().getSize();
    }
}
new Label(labelId, sizeModel);

With this, every time the page renders, sizeModel().getObejct() will be called to retrieve the value for the Label. In that other way, the Label has got a Model with a constant value.

You could even do label.setModelObject(list.size()) in the onSubmit() method.

From my ignorance on how you have built this form, I'll show you how would I do this. The List of results would be retrieved with a LoadableDetachableModel. That would be the Model of the ListView. Then, the Label can have for instance an AbstractReadOnlyModel that uses the ListViews modelObject to get its size.

public class MyForm extends Form {


    private LoadableDetachableModel resultsModel;
    private IModel searchModel;
    public MyForm(){
        searchModel = new Model();
        TextField searchTextField = new TextField("search", searchModel);
        resultsModel = new LoadableDetachableModel(){
             protected Object load(){
                 return myService.get(searchModel.getModelObject());
             }
        }
        ListView lv = new ListView("list", resultsModel){
            // ...
        }
        Label resultsCount = new Label("count", new AbstractReadOnlyModel(){
              public Object getObject(){
                   return ((List) resultsModel.getObject()).size();
              }
        })
        SubmitButton button = new SubmitButton(){
             public void onSubmit(){
                 //... No actions needed, really
             }
        }
        // add's...
    }
}

Using a LoadableDetachableModel for the ListView has the advantage of automatically detaching the Model, and therefore avoiding the whole List of results to get serialized into the Session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文