Wicketパスワードフィールドの例
「password」と「confirm password」の2つのパスワードフィールドを作成し、strong password validatorを添付して、パスワード値を次のページに渡す方法を示すWicketチュートリアル。
//Java
import org.apache.wicket.markup.html.form.PasswordTextField;
...
final PasswordTextField password = new PasswordTextField("password", Model.of(""));
form.add(password);
//HTML
1. Wicketパスワードの例
2つのパスワードフィールドを表示するユーザーページ。 パスワードチェック用に2つのバリデーターPatternValidatorとEqualPasswordInputValidatorを添付しました。
ファイル:UserPage.java
package com.example.user;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.Model;
import org.apache.wicket.validation.validator.PatternValidator;
public class UserPage extends WebPage {
//1 digit, 1 lower, 1 upper, 1 symbol "@#$%", from 6 to 20
private final String PASSWORD_PATTERN
= "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
public UserPage(final PageParameters parameters) {
add(new FeedbackPanel("feedback"));
//create a password field
final PasswordTextField password = new PasswordTextField("password",
Model.of(""));
//for properties file
password.setLabel(Model.of("Password"));
final PasswordTextField cpassword = new PasswordTextField("cpassword",
Model.of(""));
cpassword.setLabel(Model.of("Confirm Password"));
password.add(new PatternValidator(PASSWORD_PATTERN));
Form> form = new Form("userForm") {
@Override
protected void onSubmit() {
//get the entered password and pass to next page
PageParameters pageParameters = new PageParameters();
pageParameters.add("password", password.getModelObject());
setResponsePage(SuccessPage.class, pageParameters);
}
};
add(form);
form.add(password);
form.add(cpassword);
form.add(new EqualPasswordInputValidator(password, cpassword));
}
}
ファイル:UserPage.html
Wicket password Example
2. package.properties
文字列を「package.properties」に入れて、他のページ間で共有できるようにします。
ファイル:package.properties
password.Required = ${label} is required
cpassword.Required = ${label} is required
password.PatternValidator = ${label} should contains at least 1 digit, ... (omitted)
cpassword.EqualPasswordInputValidator = "${label} did not match!"

