Reputation: 25
Ive got following code to Update a User.class:
@SuppressWarnings("serial")
public class UpdateProfilePanel extends Panel{
protected ServiceClientTemp client = ((WicketApplication) (WicketApplication.get())).getClient();
protected User oldUser;
protected User newUser;
public UpdateProfilePanel(String id) {
super(id);
Form updateProfileForm = new UpdateProfileForm("updateProfileForm");
add(updateProfileForm);
}
class UpdateProfileForm extends Form {
private FormComponent formForename;
private FormComponent formSurname;
private FormComponent formEmail;
public UpdateProfileForm(String id) {
super(id);
oldUser = client.getSessionUser();
formForename = new TextField("forename1", new PropertyModel(oldUser, "forename"));
formSurname = new TextField("surname1", new PropertyModel(oldUser, "surname"));
formEmail = new TextField("email1", new PropertyModel(oldUser, "email"));
add(formForename);
add(formSurname);
add(formEmail);
}
public void onSubmit() {
newUser = new User();
newUser.setForename(formForename.getInput());
newUser.setSurname(formSurname.getInput());
newUser.setEmail(formEmail.getInput());
}
}
}
When I enter a new forename and press my submit button, the new value stays in the Textfield. For later works thats fine like this, but just for understanding: Why does he update my Textfield, when the PropertyModel refrences still to oldUser and by client.getSessionUser() I get still the oldUser. There was no update in the backend.
On the same WebPage Ive got another Panel, which gives me the actuall user information.
@SuppressWarnings("serial")
public class UserInfoPanel extends Panel {
protected ServiceClientTemp client = ((WicketApplication) (WicketApplication.get())).getClient();
protected User infoUser;
@SuppressWarnings("rawtypes")
UserInfoPanel(String id) {
super(id);
infoUser = client.getSessionUser();
add(new Label("username", new PropertyModel(infoUser, "username")));
add(new Label("surname", new PropertyModel(infoUser, "surname")));
add(new Label("forename", new PropertyModel(infoUser, "forename")));
add(new Label("email", new PropertyModel(infoUser, "email")));
add(new Label("state", new PropertyModel(infoUser, "state")));
}
}
Also this labels turn into the new value, although he gets still the oldUser by client.getSessionUser(), because the update method is not implemented yet.
Hopefully someone can explain me why the PropertyModels reference to the newUser instead of the oldUser. Why else is it like I construct my PropertyModel like PropertyMode(oldUser, ...
Upvotes: 0
Views: 2523
Reputation: 1939
That's how models work.
With
formForename = new TextField("forename1", new PropertyModel(oldUser, "forename"));
you set a reference to your oldUser for the property "forename".
Now when you submit the form, the model updates itself, so that the property "forename" of the object oldUser gets updated with the new value.
All about models here: https://cwiki.apache.org/WICKET/working-with-wicket-models.html
Upvotes: 3