2 Replies Latest reply: Oct 21, 2010 8:18 AM by 807034 RSS

    Struts2 - Dynamic values in expression while doing validations using annota

    807034
      Hi,

      I am using Struts2. I am doing validations using annotations in action class.
      I have many forms having same type of fields say zip code.

      I am adding validations like below in each action class having zip code field

      @Action(value = "/saveData")
      @Validations(
           regexFields = {
                @RegexFieldValidator(fieldName = "databean.zipCode", expression = "^([1-9]{1}[0-9]{4}(-[0-9]{4})?)$", key = "errors.zipcode.format", message = "errors.zipcode.format")
                }
           )
      public String saveData() throws Exception {
      }


      Now, in case if I need to change the expression for zip code validations, then I need to modify the same in each action class.
      I would like to know if there is any way to use java variable or property for expression so that i only need to change at one place (value of variable) and all actions get modified.


      I want something like below
      private static final String ZIP_CODE_FORMAT_EXPRESSION = "^([1-9]{1}[0-9]{4}(-[0-9]{4})?)$"
      @Action(value = "/saveData")
      @Validations(
           regexFields = {
                @RegexFieldValidator(fieldName = "databean.zipCode", expression = "${ZIP_CODE_FORMAT_EXPRESSION}", key = "errors.zipcode.format", message = "errors.zipcode.format")
                }
           )
      public String saveData() throws Exception {
      }



      Thanks
      Krishan Babbar