Servlets Spring MVC-在onBindAndValidate中添加cookie
我目前正在使用一个遗留的Spring2.5应用程序,我想做的是修改一个自定义控制器(它扩展了SimpleFormController)的验证逻辑Servlets Spring MVC-在onBindAndValidate中添加cookie,servlets,cookies,spring-mvc,Servlets,Cookies,Spring Mvc,我目前正在使用一个遗留的Spring2.5应用程序,我想做的是修改一个自定义控制器(它扩展了SimpleFormController)的验证逻辑 protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception; 我想在这个方法中做的是根据提供给我的服务类的结果编写一个cookie。但是,由于此时在控制器的工作流中,我无法访问Ht
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception;
我想在这个方法中做的是根据提供给我的服务类的结果编写一个cookie。但是,由于此时在控制器的工作流中,我无法访问HttpServletResponse
对象,因此是否有其他方法:
- 检索
对象以向其写入cookie。或者HttpServletResponse
- 使用SpringMVC中的其他工具生成cookie。我已经看过了
,但它仍然需要一个响应对象才能工作org.springframework.web.util.CookieGenerator
谢谢你抽出时间 事实证明,我仍然无法从SpringMVC2.5中找到任何直观的内置方法来实现这一点 所以我最后做的就是这个黑客。首先,我修改了控制器以调用一个实用程序函数类,该类将获取生成的服务对象(无论在何处调用),该类公开了一个与servlet兼容的
getCookie()
方法,然后将其放置在用户会话下,如下所示:
WebUtils.setSessionAttribute(request, "myResponseObjectSessionName", myResponseObject);
现在,由于它们都继承自SimpleFormController
,因此我创建了一个新的抽象类,该类仍然继承自前者,但有一个修改的HandlerRequestInternal()
方法,如下所示:
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;
}
不是很优雅,但我认为它很管用。干杯