抛出异常时重定向请求

发布于 2024-10-07 08:43:00 字数 242 浏览 10 评论 0原文

在我的应用程序(使用 spring)中,

我尝试使用 spring exusingBean 从视图中调用方法。当我尝试从视图中调用方法时,它会抛出错误。我尝试使用 HandlerExceptionResolver 捕获,但没有运气,我认为 HandlerExceptionResolver 无法处理它,因为异常没有抛出到控制器。

所以我尝试另一种方法在抛出异常时重定向请求。我认为方面有可能做到这一点。当方面抛出异常时是否可以重定向请求?

in my application (using spring),

i try to call a method from view using spring exposingBean. and when i try to invoke a method from view, it throw error. i try to catch with HandlerExceptionResolver, but no luck, i think it cannot handled by HandlerExceptionResolver because exception wasn't thrown to controller.

so i try another way to redirect the request when exception thrown. and i think aspect has possibility to do it. is it possible to redirected request when exception thrown from aspect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-10-14 08:43:00

正如您所说,当从视图内部抛出异常时,不会调用 HandlerExceptionResolver 。这些解析器非常专门针对控制器异常。

这里最好的选择是使用 HandlerInterceptor 并重写 afterCompletion 方法,该方法将包含视图抛出的异常。您也许能够从此处发送重定向,具体取决于视图是否已提交响应。

我也不明白方面对你有什么帮助。不是这项工作的工具。

然而,我对您的建议是停止在 JSP 中使用暴露的 bean。我意识到它非常方便,但这就是使用它时遇到的麻烦。我建议您的控制器组装视图所需的所有数据,将其粘贴到模型中,然后将其发送到视图。这样,视图触发异常的危险就较小,因为它已经拥有了所需的一切。

另外,如果您需要发送重定向,就像您所做的那样,那么您确实需要在视图开始执行之前执行此操作。否则,视图层可能会在抛出异常之前开始写出 HTTP 响应标头。如果发生这种情况,那么您将无法发送重定向 - 响应为“已提交”。

As you rightly say, HandlerExceptionResolver will not be invoked when an exception is thrown from inside the view. These resolvers are very specifically targetted at controller exceptions.

Your best options here are to use either a HandlerInterceptor and override the afterCompletion method, which will contain the exception thrown by the view. You may be able to send a redirect from here, dependning on whether or not the response has already been committed by the view.

I don't see how aspects would help you here, either. Not the tool for this job.

However, my advice to you is to stop using exposed bean in your JSP. I realise that it's temptingly convenient, but this is the sort of trouble you get from using it. I advise that your controller assemble all the data required by the view, stick it in the model, and send it to the view. That way, there's less danger of the view triggering an exception, since it already has everything it needs.

Also, if you need to send a redirect, as you do, then you really need to do this before the view starts executing. Otherwise, the view layer may start writing out the HTTP response headers before the exception is thrown. If this happens, then you won't then be able to send a redirect instead - the response is "committed".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文