Struts 2 - Reusing Valid Expression Validator

In struts 2, we can develop @CustomValidator , which can be used in widespread use.

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

For checking 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 complex and should work on MORE THAN ONE FIELD, we use OGNL to call the static method. The static method will check and return a boolean for example

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

Above is an example of how a Custom Expression Validation Tool! And we use @foo.bar.CalendarUtil@compareDates in the application to do this check for us.

Is there any other approach that allows you to use a custom wide validator ?! Is there any custom expression checking mechanism that can be added to struts and we can call it in action like we use @CustomValidator

+3
java validation struts2 struts-validation struts2-convention-plugin
Jan 20 '15 at 6:12
source share
2 answers

Create a custom validator (not associated with a field):

 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 the 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 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; } 
+3
Jan 20 '15 at 10:45
source share

You can use a personalized borderless validator if you need to check multiple fields. A Nelian custom validator should extend ValidatorSupport and implement the validate method. Then apply a special validator in your -validation.xml or use the @CustomValidator annotation. There you can add custom expressions like @ValidationParameter and @ValidationParameter them when performing validation. Note that this method, which I used primarily in this , responds:

 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 the validate method of the ActionSupport class. The invocation of this method is controlled by the validation hook. This verification method is known as programmatic and is used by default independently of the validators used during declarative validation.

+1
Jan 20 '15 at 2:05
source share



All Articles