如何连接 ModelAndViewDefiningException 以将错误发送到我的视图

发布于 2024-12-04 22:08:40 字数 185 浏览 1 评论 0原文

以最简单、最直接的方式,如何连接 ModelAndViewDefiningException 装饰物以将错误发送到我指定的视图?

这个人应该从: org.springframework.web.servlet.ModelAndViewDefiningException

提前非常感谢任何人!

In the easiest, most straightforward way, how do I wire up a/the ModelAndViewDefiningException doodad to send errors to my specified view?

This guy should be called from: org.springframework.web.servlet.ModelAndViewDefiningException

Much thanks to anyone in advance!

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

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

发布评论

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

评论(1

∞梦里开花 2024-12-11 22:08:40

如何连接 ModelAndViewDefiningException 以将错误发送到我的视图?

这是一个例外。你扔它!

ModelAndViewDefiningExceptionModelAndView 的异常包装器。 Spring 框架识别它、捕获它、从中提取模型和视图名称,然后转发到已解析的视图,将模型公开给视图。

...
if (someBadThingy) {
  ModelAndView modelAndView = new ModelAndView("errorView"); // "errorView" name resolved to a view by the ViewResolver of your app
  throw new ModelAndViewDefiningException(modelAndView);
}
...

您可以在处理程序处理期间随时抛出它。通常,在 Controller 内 的代码通常不会引发异常,因为控制器请求处理程序方法返回 ModelAndView。因此,您可以只返回 ModelAndView,而不是在 ModelAndView 内部抛出异常。

但有些情况下您无法返回 ModelAndView拦截器是很好的例子,因为它们的方法返回的是 voidboolean。如果拦截器内未满足某些条件,ModelAndViewDefiningException 允许您中断流程并转到错误视图。

How do I wire up ModelAndViewDefiningException to send errors to my view?

It's an exception. You throw it!

The ModelAndViewDefiningException is an exception wrapper for a ModelAndView. The Spring framework recognizes it, catches it, extracts the model and view name from it and forwards to the resolved view exposing the model to the view.

...
if (someBadThingy) {
  ModelAndView modelAndView = new ModelAndView("errorView"); // "errorView" name resolved to a view by the ViewResolver of your app
  throw new ModelAndViewDefiningException(modelAndView);
}
...

You can throw it at any time during handler processing. Normally, inside a Controller's code you don't usually throw the exception because Controller request handler methods return a ModelAndView. So instead of throwing an exception with the ModelAndView inside, you can just return the ModelAndView.

But there are situations when you can't return a ModelAndView. Interceptors are a good example of this because their methods are of void or boolean return. If some condition isn't met inside the interceptor the ModelAndViewDefiningException allows you to break the flow and go to the error view.

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