开发者_JAVA技巧I am working on an old web application which uses Struts 1.1. In order to do business validations (complex validations involving multiple form properties and invoking backend services), i wrote an ActionForm implementation which invokes a chain of validators on the ActionForm.validate method.
The chain of validators are implemented using commons chain. Everything is good, except that when an exception occurs in ActionForm.validate, it is not caught by the struts exception handler, instead the stacktrace is shown on the screen. The struts exception handler doesn't catch that
Is there anyway to avoid the stacktrace on the page and propagate the exception to struts exception handler?
Thanks
Maybe it's best that validators don't throw exceptions at all. Validations are ment to check user inputs against constraits like minChars, maxChars, password strength/verification, email syntactical correctness, isNumber and so on. If a validator fails, it should only return false - never throw an exception. See the examples at http://struts.apache.org/1.2.4/userGuide/dev_validator.html
If your validator invokes backend functions ... there is something wrong - I think. The validator should only add field errors.
If there are errors as the consequence of syntactical correct inputs (e.g. wrong user/password for a login) then your struts application (your actions) should handle this and return action errors to the user. This can be done by redirecting to error pages upon thrown exceptions (UserNotLoggedInException).
精彩评论