Struts: 一次验证两个字段
Struts: Validate two fields at once
我是 struts 的新手,我遇到了一个似乎无法解决的问题。
问题是,我在 .jsp
页面中有两个日期字段,并且都是必需的。
简而言之,我有两个必填字段,但如果它们都是空的,我将无法收到两条错误消息。唯一必须显示的消息是 "please insert the date interval",无论哪一个是空的(或者,如果它们都是空的)。
我正在使用 validation.xml
、Struts 版本 1.3
如果使用 ValidatorForm
和 validate
方法,验证两个或多个字段太容易了。
要使用声明式自定义验证器,您需要阅读 this 参考指南,其中包含用于验证两个字段的自定义验证器链接和示例。
This is an example of how you could compare two fields to see if they have the same value. A good example of this is when you are validating a user changing their password and there is the main password field and a confirmation field.
<validator name="twofields"
classname="com.mysite.StrutsValidator"
method="validateTwoFields"
msg="errors.twofields"/>
<field property="password"
depends="required,twofields">
<arg position="0" key="typeForm.password.displayname"/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
public class CustomValidator {
// ------------------------------------------------------------ Constructors
/**
* Constructor for CustomValidator.
*/
public CustomValidator() {
super();
}
// ---------------------------------------------------------- Public Methods
/**
* Example validator for comparing the equality of two fields
*
* http://struts.apache.org/userGuide/dev_validator.html
* http://www.raibledesigns.com/page/rd/20030226
*/
public static boolean validateTwoFields(
Object bean,
ValidatorAction va,
Field field,
ActionMessages errors,
HttpServletRequest request) {
String value =
ValidatorUtils.getValueAsString(bean, field.getProperty());
String property2 = field.getVarValue("secondProperty");
String value2 = ValidatorUtils.getValueAsString(bean, property2);
if (!GenericValidator.isBlankOrNull(value)) {
try {
if (!value.equals(value2)) {
errors.add(
field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
} catch (Exception e) {
errors.add(
field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
}
return true;
}
}
我是 struts 的新手,我遇到了一个似乎无法解决的问题。
问题是,我在 .jsp
页面中有两个日期字段,并且都是必需的。
简而言之,我有两个必填字段,但如果它们都是空的,我将无法收到两条错误消息。唯一必须显示的消息是 "please insert the date interval",无论哪一个是空的(或者,如果它们都是空的)。
我正在使用 validation.xml
、Struts 版本 1.3
如果使用 ValidatorForm
和 validate
方法,验证两个或多个字段太容易了。
要使用声明式自定义验证器,您需要阅读 this 参考指南,其中包含用于验证两个字段的自定义验证器链接和示例。
This is an example of how you could compare two fields to see if they have the same value. A good example of this is when you are validating a user changing their password and there is the main password field and a confirmation field.
<validator name="twofields"
classname="com.mysite.StrutsValidator"
method="validateTwoFields"
msg="errors.twofields"/>
<field property="password"
depends="required,twofields">
<arg position="0" key="typeForm.password.displayname"/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
public class CustomValidator {
// ------------------------------------------------------------ Constructors
/**
* Constructor for CustomValidator.
*/
public CustomValidator() {
super();
}
// ---------------------------------------------------------- Public Methods
/**
* Example validator for comparing the equality of two fields
*
* http://struts.apache.org/userGuide/dev_validator.html
* http://www.raibledesigns.com/page/rd/20030226
*/
public static boolean validateTwoFields(
Object bean,
ValidatorAction va,
Field field,
ActionMessages errors,
HttpServletRequest request) {
String value =
ValidatorUtils.getValueAsString(bean, field.getProperty());
String property2 = field.getVarValue("secondProperty");
String value2 = ValidatorUtils.getValueAsString(bean, property2);
if (!GenericValidator.isBlankOrNull(value)) {
try {
if (!value.equals(value2)) {
errors.add(
field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
} catch (Exception e) {
errors.add(
field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
}
return true;
}
}