Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

java - How can I validate two fields like password and confirm password in Struts 2?

I am doing web application in that I want to validate two fields in the JSP form. In registration filed I have so many fields. In that I want to validate password and confirm password fields.

Below is my code:

Action Class:

@Length(min = 6, max = 20)
@Column(name = "PERSON_PASSWORD", nullable = false, length = 20)
public String getPassword() {
    return password;
}

@Length(min = 6, max = 20)
@Column(name = "PERSON_CONFORMPASSWORD", nullable = false, length = 20)
public String getConformPassword() {
    return conformPassword;
}

Now, how can I validate the two fields contain the same data?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use a non-field custom validator to validate any number of fields. For this purpose you should create a custom validator that extend ValidatorSupport and implement validate method which is inherited from Validator interface but doesn't have default implementation. You should write this implementation to do custom validation. For example you want to create a RetypeValidator which validates two fields have the same value. It could look like

public class RetypeValidator extends ValidatorSupport {

  private String value = null;

  public String getValue() {
    return value;
  }
  public void setValue(String value) {
    this.value = value;
  }

  private String retypeValue = null;

  public String getRetypeValue() {
    return retypeValue;
  }

  public void setRetypeValue(String value) {
    retypeValue = value;
  }

  @Override
  public void validate(Object object) throws ValidationException {
    String value = (String) parse(this.value, String.class);
    String retypeValue = (String) parse(this.retypeValue, String.class);
    if (value != null && retypeValue != null && !value.equals(retypeValue))
      addActionError(getDefaultMessage());
  }
}

then you have to add this validator to the configuration in the validators.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator Config 1.0//EN"
        "http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd">

<validators>
  <validator name="retypeValidator" class="org.custom.struts.vaidators.RetypeValidator"/>
</validators>

Now, you have a custom validator name that you could use with @CustomValidator annotation.

@Validations(
    customValidators = @CustomValidator(type="retypeValidator", message="the value and retype value should be the same",
      parameters = { @ValidationParameter( name = "value", value = "${password}" ), @ValidationParameter( name = "retypeValue", value = "${conformPassword}" )})
)

Note, password and conformPassword are OGNL expressions used to parse when being validated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...