Armaggedon
Armaggedon

Reputation: 419

Is there a way to change the order of the validation: validate() vs validation.xml in Struts 2?

I have a validation.xml file to check if the fields of the form are empty and that kind of simple validation. I also have a validate() method (extended from ActionSupport) to check more complicated things.

But, when I send the form, it checks the method before the XML file, so if the fields are empty a NullPointerException appears. At least, that's what I think is happening.

So, my question is, is there a way to change the order of the validation, so the XML is checked before the method?

EDIT:

I had the idea of checking if the String is not null in the validate() method, so I can avoid the problem, but I don't think that's the wisest thing to do.

Upvotes: 2

Views: 228

Answers (2)

Roman C
Roman C

Reputation: 1

The order is always the same, which is hardcoded order.

The process of validation is performed by the ValidationInterceptor class (at least version 2.3.8).

This interceptor runs the action through the standard validation framework, which in turn checks the action against any validation rules (found in files such as ActionClass-validation.xml) and adds field-level and action-level error messages (provided that the action implements ValidationAware). This interceptor is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have already been set on the action.

This interceptor does nothing if the name of the method being invoked is specified in the excludeMethods parameter. excludeMethods accepts a comma-delimited list of method names. For example, requests to foo!input.action and foo!back.action will be skipped by this interceptor if you set the excludeMethods parameter to "input, back".

The workflow of the action request does not change due to this interceptor. Rather, this interceptor is often used in conjunction with the workflow interceptor.

NOTE: As this method extends off MethodFilterInterceptor, it is capable of deciding if it is applicable only to selective methods in the action class. See MethodFilterInterceptor for more info.


First, it checks if the declarative validation is enabled and do it, then it checks if programmatic validation is enabled and do it.

You can turn on/off each type of validation via the interceptor parameters.

Interceptor parameters:

  • alwaysInvokeValidate - Defaults to true. If true validate() method will always be invoked, otherwise it will not.
  • programmatic - Defaults to true. If true and the action is Validateable call validate(), and any method that starts with "validate".
  • declarative - Defaults to true. Perform validation based on XML or annotations.

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160181

The interceptor does check XML first, but IIRC doesn't stop validation if it find errors. I believe I have a patch for this, controlled with a flag.

I've solved this before by checking for errors in the validate method and not proceeding if errors existed.

Upvotes: 3

Related Questions