Struts 2 <s: password> Kennwortbeispiel

Struts 2 Passwortbeispiel

Laden Sie es herunter -Struts2-Password-Example.zip

In Struts 2 können Sie mit<s:password>ein HTML-Kennwortfeld erstellen. Beispielsweise können Sie "s:password" mit einem Schlüsselattribut oder einem Label- und Namensattribut deklarieren.


//or

Beide erzeugen die gleiche HTML-Ausgabe (im Standard-XHTML-Design).


  


  

Beispiel für Struts 2

Eine Seite mit den Feldern "Passwort" und "Passwort bestätigen", und führen Sie die Validierung durch, um sicherzustellen, dass das "Passwort bestätigen" mit dem "Passwort" übereinstimmt.

1. Eigenschaftendatei

global.properties

#Global messages
username = Username
password = Password
confirmPassword = Confirm Password
submit = Submit

RegisterAction.properties

#error message
username.required = Username is required
password.required = Password is required
cpassword.required = Confirm password is required
cpassword.notmatch = Confirm password is not match

2. Aktion

RegisterAction.java

package com.example.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{

    private String username;
    private String password;
    private String confirmPassword;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    //business logic
    public String execute() {

        return "SUCCESS";

    }

    //simple validation
    public void validate(){
        if("".equals(getUsername())){
            addFieldError("username", getText("username.required"));
        }
        if("".equals(getPassword())){
            addFieldError("password", getText("password.required"));
        }
        if("".equals(getConfirmPassword())){
            addFieldError("confirmPassword", getText("cpassword.required"));
        }

        if(!(getPassword().equals(getConfirmPassword()))){
            addFieldError("confirmPassword", getText("cpassword.notmatch"));
        }
    }

}

3. Seite anzeigen

Ergebnisseite mit Struts 2 "s:password" -Tag zum Erstellen eines HTML-Kennwortfelds.

register.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>





Struts 2 - password example

welcome.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



Struts 2 - password example

Password :

Confirm Password :

4. struts.xml

Link alle zusammen ~





   
   

   
    
        pages/register.jsp
    
    
        pages/welcome.jsp
        pages/register.jsp