处理 Struts 1 ActionForm 中的异常
我正在开发一个使用 Struts 1.1 的旧 Web 应用程序。为了进行业务验证(涉及多个表单属性和调用后端服务的复杂验证),我编写了一个 ActionForm 实现,它在 ActionForm.validate 方法上调用一系列验证器。
验证者链是使用公共链实现的。一切都很好,只是当 ActionForm.validate 中发生异常时,struts 异常处理程序不会捕获它,而是将堆栈跟踪显示在屏幕上。 struts 异常处理程序没有捕获是否
有办法避免页面上的堆栈跟踪并将异常传播到 struts 异常处理程序?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许验证器最好根本不抛出异常。验证用于根据 minChars、maxChars、密码强度/验证、电子邮件语法正确性、isNumber 等约束检查用户输入。如果验证器失败,它应该只返回 false - 永远不要抛出异常。请参阅 http://struts.apache.org/1.2.4/userGuide 中的示例/dev_validator.html
如果你的验证器调用后端函数......我认为有问题。验证器应该只添加字段错误。
如果由于语法正确输入而出现错误(例如,登录的用户/密码错误),那么您的 struts 应用程序(您的操作)应该处理此问题并将操作错误返回给用户。这可以通过在抛出异常 (UserNotLoggedInException) 时重定向到错误页面来完成。
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).