在Spring 3中为所有控制器的模型添加属性 [英] Add attributes to the model of all controllers in Spring 3

查看:337
本文介绍了在Spring 3中为所有控制器的模型添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spring 3应用程序中的每个视图都有一组可以依赖的属性。所以每个控制器的第一行是这样的:

Every single view in my Spring 3 app has a set of attributes they can rely on. So the first line of every controller is something like:

ControllerHelper.addDefaultModel(model, personManager, request);

在那里我会添加


  • 如果有人登录,则从数据库中检索用户对象和全名

  • 通常设置一次的变量集(例如 imagesHost

  • 访问者可以切换到的语言集合

  • 当前语言

  • 一些统计数据(例如我们系统中的总人数)

  • user object and full name retrieved from the database if person is logged in
  • set of variables which are typically set once (e.g. imagesHost)
  • set of languages a visitor can switch to
  • current language
  • some statistics (e.g. total # of people in our system)

这一切都允许每个视图显示登录用户的名字,轻松参考一个图像位置,一个语言列表和一些关于该网站的整体统计数据。

This all allows each view to display the logged in user's name, easily reference an image location, a list of languages and some overall stats about the site.

所以问题是,控制器模型对象是存储所有数据的最佳位置还是是否有一个更方便的地方,使观看访问这些信息一样容易?

So the question is, is the controller model object the best place to store all the data or is there a more convenient place which makes it just as easy for views to access this info?

其次,我真的很想不必拥有 ControllerHelper 上面的行作为每个控制器的第一行。它实际上并不总是第一行,有时我首先检查是否需要在该控制器中重定向,因为我不想浪费资源填充模型。也许过滤器或注释或一些Spring回调机制可以确保 c> ControllerHelper 代码在控制器完成之后调用但是之前 / strong>视图被渲染,如果返回重定向则跳过此视图?

And secondly, I'd REALLY love not to have to have the ControllerHelper line above as the first line in every controller. It's actually not always the first line, sometimes I first check if I need to redirect in that controller, because I don't want to waste resources filling the model for no reason. Maybe a filter or annotation or some Spring callback mechanism could make sure the ControllerHelper code is called after the controller is finished but right before the view is rendered, skipping this if a redirect was returned?

推荐答案

你可以写一个 org.springframework。 web.servlet.HandlerInterceptor 。 (或其便利子类 HandlerInterceptorAdapter

You could write an org.springframework.web.servlet.HandlerInterceptor. (or its convenience subclass HandlerInterceptorAdapter)

@See: Spring参考章节:
15.4。 1拦截请求 - HandlerInterceptor接口

它的方法有:

void postHandle(HttpServletRequest request,
                HttpServletResponse response,
                Object handler,
                ModelAndView modelAndView) throws Exception;

在控制器完成之后和渲染视图之前调用此方法。所以你可以使用它来为模型地图添加一些属性

This method is invoked after the controller is done and before the view is rendered. So you can use it, to add some properties to the ModelMap

一个例子:

/**
 * Add the current version under name {@link #VERSION_MODEL_ATTRIBUTE_NAME}
 * to each model. 
 * @author Ralph
 */
public class VersionAddingHandlerInterceptor extends HandlerInterceptorAdapter {

    /**
     * The name under which the version is added to the model map.
     */
    public static final String VERSION_MODEL_ATTRIBUTE_NAME =
                "VersionAddingHandlerInterceptor_version";

    /**        
     *  it is my personal implmentation 
     *  I wanted to demonstrate something usefull
     */
    private VersionService versionService;

    public VersionAddingHandlerInterceptor(final VersionService versionService) {
        this.versionService = versionService;
    }

    @Override
    public void postHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView) throws Exception {

        if (modelAndView != null) {
            modelAndView.getModelMap().
                  addAttribute(VERSION_MODEL_ATTRIBUTE_NAME,
                               versionService.getVersion());
        }
    }
}

webmvc-config.xml

webmvc-config.xml

<mvc:interceptors>
    <bean class="demo.VersionAddingHandlerInterceptor" autowire="constructor" />
</mvc:interceptors>

这篇关于在Spring 3中为所有控制器的模型添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆