检票口虚拟表格字段

发布于 2024-10-20 02:31:02 字数 1161 浏览 4 评论 0原文

我尝试为用户制作使用 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 组件型号:

或者 User 模型没有与password2相关的方法。遇到这样的情况该如何处理呢?

I try to make form for user to register using Wicket. I got user POJO and wicket form - this needs to have "repeat password" field which should be in no way connected to User object. But how can I do this? I

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);
}

But i get

java.lang.IllegalStateException:
Attempt to set model object on null
model of component:

or that User model has no password2 related methods. How to handle such a case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-10-27 02:31:02

这应该可以做到:

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

解释:CompoundPropertyModel将嵌套表单元素与父模型关联起来(组件名称foo映射到的bean.foo属性父模型)。您可以通过为子组件分配不同的模型来覆盖此行为。

This should do it:

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

Explanation: CompoundPropertyModel associates nested form elements with the parent model (component name foo is mapped to the bean.foo property of the parent model). You can overwrite this behavior by assigning a different model to the child component.

微凉徒眸意 2024-10-27 02:31:02

我会使用表单中的属性和 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);
}

I would have used a property in the form and a PropertyModel. This way I would have access to the field throught the getPassword2() method.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文