管理多个选项卡的 webapp 会话数据/控制器流 [英] Managing webapp session data/controller flow for multiple tabs

查看:22
本文介绍了管理多个选项卡的 webapp 会话数据/控制器流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java Web 应用程序,它在会话中存储一些数据.当用户与应用程序交互时,会话中的数据会发生变化(例如,流程由控制器管理,每个控制器有几个表单页面,在每个表单页面上,会话中的一些数据会更新,流程转到下一个表单页面).

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).

问题是一些用户打开了多个选项卡到应用程序,每个选项卡在流程中都有不同的步骤.此时会话中的数据混乱,因为选项卡共享同一个会话(应用使用 cookie 管理的会话).

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).

告诉用户使用不同的浏览器以避免共享相同的会话 ID(例如一个 Firefox 窗口和一个 IE 窗口)不是一种选择,因为肯定在某些时候有人会忘记这样做而转而使用选项卡,从而搞砸了他们的数据.

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.

添加一些验证来检测从另一个选项卡请求另一个流并向用户显示一条消息说这是不允许的,这也不是一个选项,因为它激怒了用户,我们不希望那样做,对吗?:D

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

事实上,使用另一个选项卡对用户很有用,因为他们使用应用程序的目的更高效,所以我保留了这个选项.但现在的问题是如何最好地管理多个标签的一个会话数据?

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?

推荐答案

这通常是通过为每个选项卡/窗口分配一个 windowId 并在每个请求上传递它来完成的.Jsf 通过 orchestra 支持这一点.Spring mvc 将在下一个版本中支持它.

This is usually done by assigning a windowId for each tab/window and passing it on each request. Jsf supports this via orchestra. Spring mvc will support it in the next version.

我最近需要一个简单的案例,所以我自己实现了它.用了半个小时.但是,我的范围非常有限:

I recently needed this for a simple case, so I implemented it myself. Took half an hour. However, my scope was very limited:

  • 为每个请求传递一个 windowId,并将其返回给下一个请求.第一次 - 生成它.
  • 对于要存储在会话中的任何属性,放置一个 Map,其中键是 windowId
  • 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 the windowId

这篇关于管理多个选项卡的 webapp 会话数据/控制器流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆