如何在 Struts 2 中验证密码和确认密码等两个字段? [英] How can I validate two fields like password and confirm password in Struts 2?

查看:29
本文介绍了如何在 Struts 2 中验证密码和确认密码等两个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做 Web 应用程序,因为我想验证 JSP 表单中的两个字段.在注册文件中,我有很多字段.我想验证密码并确认密码字段.

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.

下面是我的代码:

Action 类:

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?

推荐答案

您可以使用非字段自定义验证器来验证任意数量的字段.为此,您应该创建一个自定义验证器来扩展 ValidatorSupport 并实现从 Validator 接口继承但没有默认实现的 validate 方法.您应该编写此实现来进行自定义验证.例如,您要创建一个 RetypeValidator 来验证两个字段是否具有相同的值.它可能看起来像

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());
  }
}

然后您必须将此验证器添加到validators.xml 中的配置中:

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>

现在,您有了一个可以与 @CustomValidator 注释一起使用的自定义验证器名称.

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}" )})
)

注意,passwordconformPassword 是 OGNL 表达式,用于在验证时进行解析.

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

这篇关于如何在 Struts 2 中验证密码和确认密码等两个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆