spring mvc表单提交异常处理模式
我正在开发一个遗留项目,将其转换为基于注释的 MVC。我注意到 onsubmit 方法中很多时候都遵循以下模式:
public ModelAndView onSubmit(Command command) {
try {
service.doSomeBusinessLogic(command);
}
catch (ServiceException) {
//return one type of model and view here
}
//return another type of model and view here
}
直觉上,这里放置的异常处理是错误的,但我不确定 spring 给了我什么替代解决方案?有什么想法或者这不是像我想的那样的反模式吗?
I am working on a legacy project, converting it to annotation based MVC. I am noticing alot of times in the onsubmit method where the following pattern is followed:
public ModelAndView onSubmit(Command command) {
try {
service.doSomeBusinessLogic(command);
}
catch (ServiceException) {
//return one type of model and view here
}
//return another type of model and view here
}
Intuitively to me, this exception handling placed here is wrong, but I am unsure as to what alternative solutions spring gives me? Any ideas or is this not an anti-pattern like I am thinking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于不可恢复的异常,最佳实践是显示错误页面。在 web.xml 中进行配置。 Spring 的默认异常处理程序将负责将响应状态设置为 500,因此将显示 500 错误页面。
如果异常是可恢复的,那么上述方法就可以了 - 需要采取特定的操作,并将其放入 catch 子句中。
但是您应该决定哪些异常是可恢复的,哪些异常只会导致错误页面。
The best practice for unrecoverable exceptions is to show an error page. Configure that in web.xml. Spring's default exception handler will take care of setting the response status to 500, so a 500 error page will be displayed.
If the exception is recoverable, then the above approach is fine - there is a specific action to take, and it goes in the catch clause.
But you should decide which exceptions are recoverable and which should just lead to an error page.