0

I am in the middle of writing a form using 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. 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. 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.

mchandler
  • 926
  • 1
  • 12
  • 18
  • 3
    I'm not exactly sure, what you want to archieve but as a sidenote: It is often better to keep the constructor as short as possible and move the code to the `initialize()` and `onBeforeRender()` methods. initialize() is called before rendering the component for the first time, onBeforeRender() before each rendering. Maybe this might help with your problem. – Nicktar Feb 18 '13 at 09:42
  • @Nicktar I definitely see what you're saying. I'll try working with those methods to see if I can get the results I'm looking for. – mchandler Feb 18 '13 at 15:29

2 Answers2

0

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.

mchandler
  • 926
  • 1
  • 12
  • 18
0

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. 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

2 - The method you're using to load and bind the domain object to a model on the form

mchandler
  • 926
  • 1
  • 12
  • 18