java 管理多个选项卡的 webapp 会话数据/控制器流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4479995/
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
Managing webapp session data/controller flow for multiple tabs
提问by Gabriela
I have a Java web application which stores some data in the session. The data in the session changes as the user interacts with the application (e.g. flow is managed by a controller, each controller has several form pages, on each form page some data is updated in the session and flow goes to the next form page).
我有一个 Java Web 应用程序,它在会话中存储一些数据。当用户与应用程序交互时,会话中的数据会发生变化(例如,流程由控制器管理,每个控制器有几个表单页面,在每个表单页面上,会话中的一些数据会更新,流程转到下一个表单页面)。
The problem is that some users are opening more than one tab to the application, each tab with a different step in the flow. At this point data in the session is messed up since the tabs share the same session (app uses cookie managed sessions).
问题是一些用户打开了多个选项卡到应用程序,每个选项卡在流程中都有不同的步骤。此时会话中的数据混乱,因为选项卡共享相同的会话(应用程序使用 cookie 管理的会话)。
Telling the users to use different browsers to avoid sharing the same session id (e.g. one Firefox window and one IE window) is not an option since surely at some point somebody will forget to do this and instead use tabs, thus messing up their data.
告诉用户使用不同的浏览器以避免共享相同的会话 ID(例如一个 Firefox 窗口和一个 IE 窗口)不是一种选择,因为在某些时候肯定有人会忘记这样做而转而使用选项卡,从而弄乱他们的数据。
Adding some verifications that detect that another flow is requested from another tab and display a message to the user saying this is not allowed is not an option either since it pisses of the users and we don't want that do we? :D
添加一些验证来检测从另一个选项卡请求另一个流并向用户显示一条消息,说这是不允许的,这也不是一个选项,因为它激怒了用户,我们不希望那样做,对吗?:D
The fact is that using another tab is useful for the users because they are more efficient in what they use the application for, so I am keeping this option. But the question now is how best to manage the one session data for the more tabs?
事实上,使用另一个选项卡对用户很有用,因为他们在使用应用程序时更有效率,所以我保留了这个选项。但现在的问题是如何最好地管理更多选项卡的一个会话数据?
What I thought of, was to have the controller generate a token when it starts the flow and pass this token to each form page which in turn sends it back to identify itself. If another tab requests the same controller action when there is an ongoing flow then generate another token and pass that around.
我的想法是让控制器在启动流程时生成一个令牌并将此令牌传递给每个表单页面,然后将其发送回以标识自己。如果另一个选项卡在正在进行的流程中请求相同的控制器操作,则生成另一个令牌并将其传递。
Basically, I want each flow to have a token and inside the session I won't just keep one set of data but have a set of data for each token and then match requests based on the token.
基本上,我希望每个流都有一个令牌,并且在会话内我不会只保留一组数据,而是为每个令牌保存一组数据,然后根据令牌匹配请求。
Now the problem is that this approach will need a lot of rewritings to the application and I was wondering if there is a best practice for managing such a situation or can someone suggest other approaches. I am open to ideas.
现在的问题是,这种方法需要对应用程序进行大量重写,我想知道是否有管理这种情况的最佳实践,或者有人可以建议其他方法。我对想法持开放态度。
Have you encountered this situation? How did you handle it?
你遇到过这种情况吗?你是怎么处理的呢?
采纳答案by Bozho
This is usually done by assigning a windowId for each tab/window and passing it on each request. Jsf supports this via orchestra. Spring mvcwill support it in the next version.
这通常是通过为每个选项卡/窗口分配一个 windowId 并在每个请求上传递它来完成的。Jsf 通过Orchestra支持这一点。Spring mvc将在下一个版本中支持它。
I recently needed this for a simple case, so I implemented it myself. Took half an hour. However, my scope was very limited:
我最近在一个简单的案例中需要这个,所以我自己实现了它。用了半个小时。但是,我的范围非常有限:
- pass a
windowId
with each request, and return it back for the next request. The first time - generate it. - for any attribute you want to store in the session, put a
Map<String, Object>
where the key is thewindowId
- 为
windowId
每个请求传递一个,并将其返回给下一个请求。第一次 - 生成它。 - 对于您要存储在会话中的任何属性,将
Map<String, Object>
键放在windowId
回答by Drew
This is exactly what Seamwas created to handle. In Seam there's a concept called a Conversationwhich basically does exactly what you are explaining. Conversations are basically are a way to divide the Session into many pieces that can expire at some timeout. You can look at the source code for org.jboss.seam.core.Manager class to see how it's actually implemented and get inspired ;)
这正是Seam 的创建目的。在 Seam 中,有一个叫做Conversation的概念,它基本上完全符合您的解释。对话基本上是一种将 Session 分成许多可以在某个超时到期的部分的方法。您可以查看 org.jboss.seam.core.Manager 类的源代码,了解它是如何实际实现的并从中获得灵感;)
回答by Melv
Depending on the complexity of your application, you may want to investigate implementing tabs within your application. This gives you wholesale control over the flow, while still providing users with the functionality they want. I'd argue it's, bugwise, the most robust solution, since you won't have a dependency on the way the browser handles sessions, minimising the number of "known unknowns".
根据应用程序的复杂性,您可能需要研究在应用程序中实现选项卡。这使您可以全面控制流程,同时仍为用户提供他们想要的功能。我认为它是最可靠的解决方案,因为您不会依赖浏览器处理会话的方式,从而最大限度地减少“已知未知数”的数量。
Of course, there'll be potentially a large upfront cost to this, depending on how your application is structured. Without more information about your app, you're the best placed person to decide.
当然,这可能会产生很大的前期成本,具体取决于您的应用程序的结构。如果没有关于您的应用程序的更多信息,您是最适合做出决定的人。
回答by Rudy
You can also try to wrap your application inside Adobe Air
您也可以尝试将您的应用程序封装在Adobe Air 中
And then limit your web application to be only accessable from this air. By doing this you dont need to consider the web browser fragmentation and their unique behaviour.
然后将您的 Web 应用程序限制为只能从这个空中访问。通过这样做,您无需考虑 Web 浏览器碎片及其独特的行为。