I don't understand conception of Struts2 validation in next case :
My application consists of 2 actions:
- login.action
- drive.action
I can run drive.action from browser command line without filling user and password in login.action
How can I implement validation code which prevents the run of drive.action from command line if user hasn't successfully filled user and password in login.action?
It is simple, you map the validators to the fields via the validation configuration file, or via annotations. Then apply a
validationinterceptor to the action via referencing it explicitly or implicitly via the interceptor stack, custom stack ordefaultStack.When validation started it invokes the validation manager to perform actual validation and save errors to the
ValidationAwareaction.Your action should implement this interface, or just extend the
ActionSupportwhere it's already implemented, to save the errors. Thenworkflowinterceptor checks for those errors and if found any of them redirect to theINPUTresult, if no errors found the action invocation is executed. You may also add a programmatic validation to the action by implementingValidateableinterface, whichActionSupportis implemented by default, hence to override thevalidate()method(s).As a supplement to XML based validation you could also apply annotation based configuration. This only the server-side validation, the client-side validation applied to the browser enabled javascript via Struts tags used for rendering a validation content to the page being validated.
All of this concept is not applicable to the action which requires authentication (unless the authentication interceptor is applied to the action). If you use JAAS authentication, then you should consider your action to implement
PrincipalAwareor userolesinterceptor to restrict access to the action which checks theisUserInRole(). You may useAction.LOGINresult to return to the login page in authentication interceptor if the user is not authenticated like in Is there a way to redirect to another action class without using onstruts.xmlexample.