Java 如何在 Spring MVC 中存储会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1367651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:14:08  来源:igfitidea点击:

How to store session in Spring MVC

javaspring-mvchttpsession

提问by dakull

What's the best way of storing session related data of a user (like, for example a log of recent actions a user has done) in a Spring MVC (2.5) web application ?

在 Spring MVC (2.5) web 应用程序中存储用户的会话相关数据(例如,用户最近完成的操作的日志)的最佳方式是什么?

Using the classic javax.servlet.http.HttpSession or by specifying scope="session" in controller beans, and storing the data in a session object ?

使用经典的 javax.servlet.http.HttpSession 或通过在控制器 bean 中指定 scope="session" 并将数据存储在会话对象中?

采纳答案by skaffman

Session-scoped beans (using scope="session") is the cleanest approach. This removes the need to interact with the session yourself.

会话范围的 bean(使用scope="session")是最干净的方法。这消除了自己与会话交互的需要。

If you want to autowire a session-scoped bean in to the controller, you either need to make the controller session-scoped itself, or use a scoped-proxy to wire it into a singleton controller, as described here. Either approach is valid.

如果要将会话范围的 bean 自动连接到控制器,则需要使控制器本身具有会话范围,或者使用范围代理将其连接到单例控制器中,如此处所述。任何一种方法都是有效的。