Wicket:PropertyModel更新

时间:2012-01-15 13:21:20

标签: model wicket

我有以下代码来更新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());
    }
}



  }

当我输入新的名字并按下我的提交按钮时,新值将保留在文本字段中。对于后来的工作,这很好,但只是为了理解:为什么他更新我的文本字段,当PropertyModel仍然反对oldUser和client.getSessionUser()我仍然是旧的用户。后端没有更新。

在同一个WebPage上,我有另一个Panel,它为我提供了实际的用户信息。

 @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")));

}

  }

此标签也变为新值,尽管他仍然通过client.getSessionUser()获取oldUser,因为更新方法尚未实现。

希望有人可以解释为什么PropertyModels引用newUser而不是oldUser。为什么我像构建PropertyMode(oldUser,...

)一样构建我的PropertyModel

1 个答案:

答案 0 :(得分:3)

这就是模型的工作方式。

使用

 formForename = new TextField("forename1", new PropertyModel(oldUser, "forename"));

您为属性“forename”设置了对oldUser的引用。

现在,当您提交表单时,模型会自行更新,以便使用新值更新对象oldUser的属性“forename”。

所有关于这里的模型:https://cwiki.apache.org/WICKET/working-with-wicket-models.html