0

我目前正在使用旧的 Spring 2.5 应用程序,我想做的是修改自定义控制器(扩展 SimpleFormController)的验证逻辑。

protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception;

我想在这个方法中做的是根据我提供的服务类的结果编写一个 cookie。但是,由于此时在控制器的工作流程中我无权访问该HttpServletResponse对象,是否还有其他方法可以:

  • 要么检索HttpServletResponse对象以向其写入 cookie。或者。
  • 使用 Spring MVC 中的一些其他工具来生成 cookie。我看过org.springframework.web.util.CookieGenerator,但它仍然需要一个响应对象才能工作。

我感谢任何人都可以提供的帮助。

感谢您的时间!

4

1 回答 1

0

事实证明,我仍然无法在 Spring MVC 2.5 中找到任何直观的内置方法来执行此操作。

所以我最终做的是这个黑客。首先,我修改了我的控制器以调用一个实用函数类,该类将获取生成的服务对象(无论它在哪里被调用),它公开了一个与 servlet 兼容的getCookie()方法,然后将它放在用户的会话下,如下所示:

WebUtils.setSessionAttribute(request, "myResponseObjectSessionName", myResponseObject);

现在,由于它们都继承自SimpleFormController,因此我创建了一个新的抽象类,它仍然继承自前者,但有一个修改过的handleRequestInternal()方法,如下所示:

public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

    // let the controller do it's job
    ModelAndView result = handleRequestInternal(request, response, errors);

    // and then use a helper class to inspect the session, find the session attribute
    // 'myResponseObjectSessionName', and set any cookies left in case it does exist at 
    // this point in time.
    new ProcessServiceObject().setServiceResponseCookie(request, response);

    return result;
}

不是很优雅,但我认为它有效。干杯。

于 2012-08-10T17:09:06.047 回答