Struts 2 - reusing Custom Expression Validator

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 06:33:01

问题


In struts 2 we can develop @CustomValidator which can be used in application wide

@CustomValidator(type = "CustomerNumberValidator", fieldName = "customerNo")

For validation MORE THAN ONE FIELD we use @ExpressionValidator

@ExpressionValidator(expression = 
"( (!''.equals(account.firstName) && (!''.equals(account.lastName) )
   || (presonalAccount == false)", 
   key = "validate.account.name")

If the expression is too complicated and needs to work on MORE THAN ONE FIELD we use OGNL to call static method. The static method will do the validation and return a boolean for example

@ExpressionValidator(expression = "@foo.bar.CalendarUtil@compareDates(fromDate,toDate)", key = "validate.date.before")

Above is some how a Custom Expression Validator ! And we use @foo.bar.CalendarUtil@compareDates in application wide to make this validation for us.

Is there another approach which enables us to use a custom wide validator?! Is there any custom expression validator which can be added to struts and we can call it in action in the way we use @CustomValidator


回答1:


Create a Custom Validator (not field related):

public final class CompareDatesValidator extends ValidatorSupport {
    private String fromDate; // getter and setter
    private String toDate;   // getter and setter    

    @Override
    public void validate(Object o) throws ValidationException {
        Date d1 = (Date)parse(fromDate, Date.class);
        Date d2 = (Date)parse(toDate, Date.class);

        if (d1==null || d2==null || d2.before(d1)){
            addActionError(getDefaultMessage());
        }
    }
}

Register the custom validator in validators.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
     "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
     "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
    <validator name="compareDatesValidator" 
              class="org.foo.bar.CompareDatesValidator"/>
</validators>

Use the validator in an action:

private Date startDate; // getter and setter
private Date endDate;   // getter and setter

@Validations(
    customValidators={
        @CustomValidator(type="compareDatesValidator", 
            message="Dates provided are not valid."
            parameters={
                @ValidationParameter(name="fromDate", value="${startDate}"), 
                @ValidationParameter(name="toDate",   value="${endDate}")})})
public String execute(){
    return SUCCESS;
}



回答2:


You can use non-field custom validator if you need to validate several fields. Non-field custom validator should extend ValidatorSupport and implement validate method. Then apply a custom validator in your -validation.xml or use @CustomValidator annotation. There you can add custom expressions as @ValidationParameter and parse them when you doing validation. Note, that this technique I used firstly in this answer:

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

Another option is to override validate method of the ActionSupport class. The call to this method is controlled via validation interceptor. This method of validation is known as programmatic and is used by default regardless of validators applied during declarative validation.



来源:https://stackoverflow.com/questions/28038827/struts-2-reusing-custom-expression-validator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!