Beispiel für ein Wicket-Passwort

Beispiel für ein Wicket-Passwortfeld

Wicket-Tutorial, das Ihnen zeigt, wie Sie zwei Kennwortfelder erstellen - "password" und "confirm password", einstrong password validator anhängen und den Kennwortwert an die nächste Seite übergeben.

//Java
import org.apache.wicket.markup.html.form.PasswordTextField;
...
final PasswordTextField password = new PasswordTextField("password", Model.of(""));
form.add(password);

//HTML

1. Wicket Passwort Beispiel

Eine Benutzerseite zum Rendern von zwei Kennwortfeldern. Zwei Validatoren,PatternValidator undEqualPasswordInputValidator, wurden zur Kennwortprüfung angehängt.

Datei: 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));

    }
}

Datei: UserPage.html






    

Wicket password Example

:

:

2. package.properties

Setzt eine Zeichenfolge in ein „package.properties“, damit sie unter den anderen Seiten geteilt werden kann.

Datei: 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!"

3. Demo

Wenn das Passwort nicht dem Muster für reguläre Ausdrücke entspricht:

wicket pattern error

Wenn das Passwort und das Bestätigungspasswort nicht übereinstimmen:

wicket password error

Laden Sie es herunter -Wicket-password-example.zip (8 KB)