Wicket假形式领域

时间:2011-03-04 16:23:11

标签: java wicket

我尝试为用户创建表单以使用Wicket进行注册。我有用户POJO和wicket表单 - 这需要有“重复密码”字段,该字段不应该与User对象连接。但是我怎么能这样做呢?我

public class RegisterForm extends Form<User> {

private static final long serialVersionUID = -9071906666130179515L;

public RegisterForm(String id) {
    super(id, new CompoundPropertyModel<User>(new User()));

    PasswordTextField pass  = new PasswordTextField("password");
    pass.setType(String.class);

    PasswordTextField pass2 = new PasswordTextField("password2");
    pass2.setType(String.class);
    pass2.setDefaultModelObject("");

    add(new EqualPasswordInputValidator(pass, pass2));

    add(new TextField<String>("login")
                .setType(String.class)
                .setRequired(true)
                .add(new PatternValidator("[a-z0-9]*")));

    add(new TextField<String>("email")
                 .setType(String.class)
                 .add(EmailAddressValidator.getInstance()));

    add(pass);

    add(pass2);
}

但是我得到了

  

java.lang.IllegalStateException:   尝试将模型对象设置为null   组件模型:

或用户模型没有与password2相关的方法。如何处理这种情况?

2 个答案:

答案 0 :(得分:5)

这应该这样做:

PasswordTextField pass2 = new PasswordTextField("password2", Model.of(""));

说明:CompoundPropertyModel将嵌套表单元素与父模型相关联(组件名称foo映射到父模型的bean.foo属性)。您可以通过为子组件指定其他模型来覆盖此行为。

答案 1 :(得分:0)

我会使用表单中的属性和PropertyModel。这样我就可以通过getPassword2()方法访问该字段。

public class RegisterForm extends Form<User> {

private static final long serialVersionUID = -9071906666130179515L;

// password2 Property
protected String password2 = "";

public String getPassword2() {
    return password2;
}

public void setPassword2(String password2) {
    this.password2 = password2;
}
// end password2 Property

public RegisterForm(String id) {
    super(id, new CompoundPropertyModel<User>(new User()));

    PasswordTextField pass  = new PasswordTextField("password");
    pass.setType(String.class);

    // add new PropertyModel
    PasswordTextField pass2 = new PasswordTextField("password2", new PropertyModel<String>(this, "password2"));

    add(new EqualPasswordInputValidator(pass, pass2));

    add(new TextField<String>("login")
                .setType(String.class)
                .setRequired(true)
                .add(new PatternValidator("[a-z0-9]*")));

    add(new TextField<String>("email")
                 .setType(String.class)
                 .add(EmailAddressValidator.getInstance()));

    add(pass);

    add(pass2);
}