0

In my login page I am trying to bind enter key press event with password textfield. I have managed to fire the event listener. But the problem I am facing is the value of the textfield username is returning null, but the password field's value is not null. The code is given below:

public class LoginForm extends Form<Void> {
    private transient final ValueMap properties = new ValueMap();

    public LoginForm(final String formId) {
        super(formId);

        final TextField<String> username = new TextField<String>("username", new PropertyModel<String>(getProperties(), "username"));
        add(username);

        final TextField<String> password = new TextField<String>("password", new PropertyModel<String>(getProperties(), "password"));
        add(password);

        password.add(new AjaxFormComponentUpdatingBehavior("onkeypress") {

            @Override
            protected void onComponentTag(ComponentTag tag) {
                Component component = getComponent();
                if (component.isEnabled() && component.isEnableAllowed()) {
                    CharSequence handler = generateCallbackScript(new AppendingStringBuffer("wicketAjaxPost('").append(getCallbackUrl()).append("', wicketSerialize(Wicket.$('" + getComponent().getMarkupId() + "'))"));
                    String event = "if (event.keyCode == 13) {" + handler.toString() + "};";
                    tag.put("onkeypress", event);
                }
            }

            @Override
            protected void onUpdate(AjaxRequestTarget target) {                 
                System.out.println(getUsername());
                System.out.println(getPassword());
            }
        });
    }

    public ValueMap getProperties() {
        return properties;
    }

    private String getPassword() {
        return getProperties().getString("username");
    }

    private String getUsername() {
        return getProperties().getString("password");
    }
}

The system.out is printing

null
password

I also tried to use username.getDefaultModelObject() but that is also returns null.

Any information will be very helpful to me.

Thanks.

4

1 回答 1

1

您需要通过 Javascript 提交您的登录表单。这将触发通常的 Wicket 表单处理并将值存储在模型中。您应该能够通过以下方式提交表格:

document.getElementById('myform').submit(); 

但是,我还没有测试过。

于 2012-05-22T09:49:33.857 回答