Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The difference is only if Struts discover validation annotations while intercepting the action it processes those annotations to perform validations by applying validation rules exposed by annotations. The same thing is when parsing <code>-validation.xml</code>. You can use both validation methods xml based and annotation based together, or with addition to custom validation (excluding custom validators). </p> <p>For example, if I have a <code>phone</code> field and I want to validate it is not empty and contains a predefined format I will just put two annotations on it. </p> <pre><code> private String phone; public String getPhone() { return phone; } @RequiredStringValidator(type= ValidatorType.FIELD, message="Phone required.") @RegexFieldValidator(type= ValidatorType.FIELD, message="Invalid Phone", regexExpression="\\([0-9][0-9][0-9]\\)\\s[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]") public void setPhone(String phone) { this.phone = phone; } </code></pre> <p>then I have an <code>execute</code> action I <em>don't want</em> to validate</p> <pre><code> @SkipValidation public String execute() throws Exception { </code></pre> <p>then I have another action <code>save</code> that I want to validate <code>questions</code> but I don't want to validate a <code>phone</code>.</p> <pre><code> private String myQuestionq1; private String myQuestionq2; public String getMyQuestionq1() { return myQuestionq1; } public void setMyQuestionq1(String myQuestionq1) { this.myQuestionq1 = myQuestionq1; } public String getMyQuestionq2() { return myQuestionq2; } public void setMyQuestionq2(String myQuestionq2) { this.myQuestionq2 = myQuestionq2; } @Action(value="save", results = { @Result(name="input", location = "/default.jsp"), @Result(name="back", type="redirect", location = "/") },interceptorRefs = @InterceptorRef(value="defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true"})) @Validations(requiredFields = { @RequiredFieldValidator(type = ValidatorType.FIELD, fieldName = "myQuestionq1", message = "You must enter a value for field myQuestionq1."), @RequiredFieldValidator(type = ValidatorType.FIELD, fieldName = "myQuestionq2", message = "You must enter a value for field myQuestionq2.") }) public String save() throws SQLException { </code></pre> <p>this will execute only validators on this action.</p> <p>More examples you could always find on Apache web site: </p> <h2><a href="http://struts.apache.org/release/2.3.x/docs/validation.html#Validation-UsingAnnotations" rel="nofollow">Validation using annotations examples.</a></h2>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload