Spring MVC:将值插入到我的所有 ModelAndView 中

发布于 2024-09-12 10:36:16 字数 160 浏览 1 评论 0原文

我有一个中型 Spring 应用程序,我想以横切方式将键/值对插入到我的所有 ModelAndView 中(很像 AOP)。

动机是将所有类型的数据添加到我的所有页面(配置值、内部版本号等)。

完成此操作最简单的方法是什么?我更喜欢没有 AOP,但如果需要的话我准备使用它。

I have a mid-size Spring application and I want to insert key/value pairs into all my ModelAndViews in a cross cutting fashion (much like AOP).

The motivation is to add all kind of data to all my pages (configuration values, build number, etc).

What is the easiest way to have this done? I prefer without AOP but I am ready to use it if necessary.

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

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

发布评论

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

评论(1

就是爱搞怪 2024-09-19 10:36:16

您在这里有多种选择。

  1. 编写自定义 HandlerInterceptor将其添加到配置,并重写 postHandle() 方法,将数据添加到提供的 ModelAndView
  2. 如果您的数据是静态的,那么您可以使用 UrlBasedViewResolverattributes 属性将其添加到 ViewResolver 中。这仅对于可以放入 beans 文件中的基于静态字符串的配置真正有用。
  3. 使用 @ModelAttribute 注释控制器中的方法。此类带注释的方法返回的任何对象都会自动添加到模型中。这仅适用于由该控制器类中的 @Requestmapping 注释方法处理的请求。

You have several options here.

  1. Write a custom HandlerInterceptor and add it to the config, and override the postHandle() method, adding your data to the supplied ModelAndView.
  2. If your data is static, then you might be able to add it to the ViewResolver using the attributes property of UrlBasedViewResolver. This is only really useful for static string-based config that you can put into your beans file.
  3. Annotated a method in your controller(s) with @ModelAttribute. Any object returned by such annotated methods are added to the model automatically. This will only be done for requests handled by @Requestmapping-annotated methods in that controller class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文