本文介绍了管理多个选项卡的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通过支持此功能。 将在下一版本中支持它。

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< String,Object> 密钥是 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会话数据/控制器流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:49