If you extend the ValidatorSupport
class in Struts 2 can you have instance variables on the class? Can it have state or does it have to be stateless?
I know action classes aren't singletons and can have state but I'm not sure about their associated validators.
I need to know if I can have an instance variable within a validator that extends ValidatorSupport
. For example:
public class SomeValidator extends ValidatorSupport {
private boolean alreadyHasErrorOnPage;
}
If the validators are signlestons, using alreadyHasErrorOnPage since it would result in a race condition and never a consistent default state for each request/response. If they aren't singletons and a new SomeValidator instance is created for each request/response then using alreadyHasErrorOnPage would be safe.
Take the following with a grain of salt because I'm not sure how much of it is specifically related to the project I'm currently working on.
Validators are singles on our project. I went in and debugged the application and found instance members to not be at a default state after a second request/response. Essentially they carry over the value from the first or previous request/response.
The reason I'm still not sure is because our project seems to have wrapped and ValidatorSuppport and exposed an interface that our validators implement. Within our codebase there seems to be code that stores an instance of the validators in a map essentially making them singletons. I haven't been able to determine if stock struts2 behaves in the same manner.