java - Custom validator does not set parameter values to the properties of the validator in Struts2 -


i'm trying build custom validator in struts2 (2.3.16) follows.

package validator;  import actions.discountaction; import com.opensymphony.xwork2.validator.validationexception; import com.opensymphony.xwork2.validator.validators.validatorsupport; import org.joda.time.datetime;  public final class discountdatevalidator extends validatorsupport {     private datetime startdate;     private datetime enddate;      //getters , setters.      @override     public void validate(object o) throws validationexception     {         system.out.println("startdate = "+startdate);         system.out.println("enddate = "+enddate);         system.out.println((o instanceof discountaction));     } } 

the validators.xml file looks following.

<?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="discountdatevalidator"                 class="validator.discountdatevalidator"/> </validators> 

and validator used in action follows.

@namespace("/admin_side") @resultpath("/web-inf/content") @parentpackage(value="struts-default") public final class discountaction extends actionsupport implements serializable, validationaware, modeldriven<discount> {     @autowired     private final transient discountservice discountservice=null;     private static final long serialversionuid = 1l;      private discount entity=new discount();      @override     public discount getmodel() {         return entity;     }      @validations(         customvalidators={             @customvalidator(type="discountdatevalidator", parameters={                 @validationparameter(name="startdate", value="${entity.discountstartdate}"),                  @validationparameter(name="enddate", value="${entity.discountenddate}")},              message="xxx zzz aaa")})     @action(value = "adddiscount",     results = {         @result(name=actionsupport.success, type="redirectaction", params={"namespace", "/admin_side", "actionname", "discount", "currentpage", "${currentpage}", "message", "${message}", "editid", "${editid}", "status", "${status}"}),         @result(name = actionsupport.input, location = "discount.jsp")},     interceptorrefs={@interceptorref(value="defaultstack", params={"params.acceptparamnames", "editid, discountid, discountcode, discountpercent, discountstartdate, discountenddate, currentpage, rowcount, totalpages, status", "validation.validateannotatedmethodonly", "true"})})     public string insert(){         return actionsupport.success;     } } 

the validate() method of discountdatevalidator invoked on submission of form, values of startdate , enddate however, null, o instanceof discountaction evaluates true.

what overlooking here?

it because ${discountstartdate} , ${discountenddate} (the value attribute of @validationparameter) directly set string properties of validator need parse manually using object parse(string expression, class type) method in validate() method.

such expressions evaluated ognl expressions parse() method. validator after these changes follows.

package validator;  import actions.discountaction; import com.opensymphony.xwork2.validator.validationexception; import com.opensymphony.xwork2.validator.validators.validatorsupport; import org.joda.time.datetime;  public final class discountdatevalidator extends validatorsupport {     private string startdate; //set ${discountstartdate}     private string enddate;   //set ${discountenddate}      public string getstartdate() {         return startdate;     }      public void setstartdate(string startdate) {         this.startdate = startdate;     }      public string getenddate() {         return enddate;     }      public void setenddate(string enddate) {         this.enddate = enddate;     }      @override     public void validate(object o) throws validationexception     {         datetime discountstartdate = (datetime)parse(startdate, datetime.class);         datetime discountenddate = (datetime)parse(enddate, datetime.class);          system.out.println("startdate = "+discountstartdate);         system.out.println("enddate = "+discountenddate);         system.out.println((o instanceof discountaction));          //perform validation required using these values.     } } 

getfieldvalue("discountstartdate", o) , getfieldvalue("discountenddate", o) work can retrieve field values directly (i'm not sure side effects of method in such kind of validators).

${entity.discountstartdate} , ${entity.discountstartdate} not required (entity should excluded included in question).


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -