Spring MVC:将值插入到我的所有 ModelAndView 中
我有一个中型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里有多种选择。
HandlerInterceptor
和 将其添加到配置,并重写postHandle()
方法,将数据添加到提供的ModelAndView
。UrlBasedViewResolver
的attributes
属性将其添加到ViewResolver
中。这仅对于可以放入 beans 文件中的基于静态字符串的配置真正有用。@ModelAttribute
注释控制器中的方法。此类带注释的方法返回的任何对象都会自动添加到模型中。这仅适用于由该控制器类中的 @Requestmapping 注释方法处理的请求。You have several options here.
HandlerInterceptor
and add it to the config, and override thepostHandle()
method, adding your data to the suppliedModelAndView
.ViewResolver
using theattributes
property ofUrlBasedViewResolver
. This is only really useful for static string-based config that you can put into your beans file.@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.