简体   繁体   English

在Spring验证方法中访问会话

[英]Accessing session in Spring validate method

I am migrating my Struts application to Spring. 我正在将Struts应用程序迁移到Spring。 While migrating I came across the validate method of Struts which needs to be migrated in Spring. 迁移时,我遇到了Struts的validate方法,该方法需要在Spring中进行迁移。 The Struts validate method accepts parameters as ActionMapping mapping, HttpServletRequest req and using the req object session was created in validate method of Struts. Struts的validate方法接受参数作为ActionMapping映射,HttpServletRequest req,并在Struts的validate方法中使用req对象创建会话。 But the validate method of Spring only accepts parameters: Object target, Errors errors . 但是Spring的validate方法仅接受参数: Object target,Errors errors So how can I create a session object in the validate method of Spring ? 那么如何在Spring的validate方法中创建会话对象? Any kind of help is appreciated. 任何帮助都将受到赞赏。

Thanks! 谢谢!

You can obtain the session by doing the following 您可以通过执行以下操作获取会话

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = attr.getRequest().getSession();

I do agree with Prashant that a one for one migration might not be the best way forward but without more context its hard to say 我确实同意Prashant的观点,即一对一迁移可能不是最好的方法,但是如果没有更多背景信息,这很难说

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM