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.