When is validate method called in struts2




















For example you put the validation interceptor. To your question: if you parameter didn't parsed correctly you will have value of 0 in the execute method. So, you decide what result to return. You can also check the value in the validate method if the validation interceptor placed after params interceptor do parse or GenericValidator.

The validation framework requires the result named "input" in the action configuration exist, if the validation errors occurred or you added them in the validate method via addActionError and addFieldError then by default this result will be returned by the action before its execution. Results are placed inside the action annotation, there could be many results separated by comma, I have remained only one that is important to validation. The referenced interceptor stack is a minimal stack that processes the validation, it's also a subset of the default stack.

If you put annotation on action then method execute will call, otherwise you need to put the action annotation on the method. There are many possibilities to configure action mapping and method execution. You may also add result "input" like in this tutorial, actions are mapped by convention, or use the validation annotations to perform validation like in this example. Actually I didn't get what kind of annotations you need.

Now do not enter any required information, just click on Submit button. Enter the required information but enter a wrong From field, let us say name as "test" and age as 30, and finally click on Submit button. If any errors have been added, then Struts 2 will not proceed to call the execute method.

Rather the Struts 2 framework will return input as the result of calling the action. Hence, when validation fails and Struts 2 returns input , the Struts 2 framework will redisplay the index. Since, we used Struts 2 form tags, Struts 2 will automatically add the error messages just above the form filed. These error messages are the ones we specified in the addFieldError method call. The addFieldError method takes two arguments.

The first, is the form field name to which the error applies and the second, is the error message to display above that form field. To handle the return value of input we need to add the following result to our action node in struts. The second method of doing validation is by placing an xml file next to the action class. Struts2 XML based validation provides more options of validation like email validation, integer range validation, form validation field, expression validation, regex validation, required validation, requiredstring validation, stringlength validation and etc.

The xml file needs to be named '[action-class]'-validation. So, in our case we create a file called Employee-validation. To resolve the JAR dependency, you add the following dependency on your pom. Or you can directly download from Struts website click here.

Struts 2 provides an interface Validatable and ValidationAware implemented by the ActionSupport class. We can override the validate method of Validateable interface to perform the validation. The ValidationAware interface can accept the field level or action class level error messages. The field level messages are kept in Map and Action class level messages are kept in the collection.

It should be implemented by the action class to add an error message.



karalopa1982's Ownd

0コメント

  • 1000 / 1000