简体   繁体   English

使用Wicket表单时,如何更好地处理Wicket模型的加载?

[英]How should I better handle loading of Wicket Models when using Wicket forms?

I am in the middle of writing a form using Apache Wicket. 我正在使用Apache Wicket编写表单。 The same page/class is used to add a new item to the database AND to edit an existing record in the database. 使用相同的页面/类将新项目添加到数据库,并编辑数据库中的现有记录。 Of course, since the page constructor is called only once, the model is always set to whatever record is initially loaded on the page, or a new record if we're not editing an existing one. 当然,由于页面构造函数仅被调用一次,因此模型始终设置为页面上最初加载的任何记录,如果我们不编辑现有记录,则将其设置为新记录。

I have found a number of ways to dynamically load data, but they seem verbose and a little clunky. 我发现了许多动态加载数据的方法,但是它们看起来很冗长而且有些笨拙。 I suspect there is a best practice for handling a scenario like this. 我怀疑有处理这种情况的最佳实践。

For reference, here is some edited code: 作为参考,下面是一些编辑后的代码:

public class JobManagement extends WebPage {

private static final long serialVersionUID = 1L;
private long jobId = 0;

protected void setJobId(long id) {
    this.jobId = id;
}

protected long getJobId() {
    return jobId;
}

public JobManagement() {
    LoadableDetachableModel<Job> jobModel = new LoadableDetachableModel<Job>() {

        private static final long serialVersionUID = 1L;

        @Override
        protected Job load() {
            Job job = (Job) EntityFactory.getInstance().getBean("job");

            // if we're editing an existing job, load the object
            if (jobId >= 1) {
                job.load(jobId);
            }

            return job;
        }

    };

    add(new FeedbackPanel("feedbackPanel"));

    Form<Job> jobForm = new Form<Job>("jobForm") {

        private static final long serialVersionUID = 1L;

        @Override
        public void onSubmit() {
            // Handles the form submit...

        }

    };

    add(jobForm);

    jobForm.setModel(new CompoundPropertyModel<Job>(jobModel));
    // SNIP ... All my form fields go here!
    jobForm.add(new Button("submit"));

}

} }

I'm using a LoadableDetachableModel, but it's not entirely clear to me how to best handle loading it dynamically whenever the page is rendered. 我正在使用LoadableDetachableModel,但是我尚不清楚如何在呈现页面时最好地动态地动态加载它。 I've attempted to load a new instance of a Model, override the getObject() class which returns my LoadableDetachableModel, but there's something that feels very wrong about that. 我试图加载一个Model的新实例,重写getObject()类,该类返回我的LoadableDetachableModel,但是有些事情对此感到很不对劲。 Any input would be appreciated. 任何输入将不胜感激。 I've been trying to feel my way through this framework via online documentation exclusively, so forgive my evident lack of familiarity. 我一直试图通过在线文档完全了解这个框架,因此请原谅我明显的不熟悉。

To answer my own question, the problem I was experiencing was that the model that was bound to the form appeared to persist every time I returned to the page. 为了回答我自己的问题,我遇到的问题是,每次返回页面时,绑定到表单的模型似乎都会保留下来。 This led me to believe the problem was how I was managing models, but the problems was actually related to how I was linking to pages. 这使我相信问题出在我如何管理模型,但是问题实际上与我如何链接到页面有关。

The page above was being linked to as follows: 上面的页面被链接如下:

Link<String> link = new BookmarkablePageLink<String>("addLink", MyAddClass.class);

While that is an acceptable approach in some cases, it is not the correct approach in this particular case. 尽管在某些情况下这是可以接受的方法,但在此特定情况下这不是正确的方法。 What should have happened was this: 应该发生的事情是这样的:

Link<String> link = new Link<String>("addLink") {
    public void onClick() {
        setResponsePage(new MyAddClass());
    }
}

When links are handled dynamically in this way, my application behaves as intended. 当以这种方式动态处理链接时,我的应用程序将按预期运行。 Thanks to everyone who pitched in to help open my eyes to this fundamental problem with my links. 感谢所有参与我工作的人,以帮助我打开我的链接解决这个根本问题的机会。

While the link method (previous answer) could pose a problem and often does depending on how you implement a link, the actual problem in this case was with the "EntityFactory" that was being used to load the instance of the domain object. 尽管链接方法(先前的答案)可能会引起问题,并且通常取决于实现链接的方式,但在这种情况下,实际的问题是用于加载域对象实例的“ EntityFactory”。 In this case, it was providing cached data instead of instantiating a new instance. 在这种情况下,它提供的是缓存数据,而不是实例化新实例。

To sum up, things to evaluate in cases like these where the form's model object doesn't appear to reset when you browse to the form a second time: 综上所述,在类似情况下,当您再次浏览到表单时,表单的模型对象似乎没有重置时,需要评估的内容:

1 - The link strategy you're using to get to the page 1-您用于访问该页面的链接策略

2 - The method you're using to load and bind the domain object to a model on the form 2-您用于将域对象加载并绑定到表单上的模型的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM