Spring MVC @Sessionattributes问题在多个浏览器选项卡中 [英] Spring MVC @Sessionattributes issue in multiple browser tabs

查看:230
本文介绍了Spring MVC @Sessionattributes问题在多个浏览器选项卡中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用spring的最新版本.

We are using latest version of spring. .

我们正在使用spring mvc的@Session属性在会话范围内存储数据.

We are uding @Sessionattributes of spring mvc to stote data in session scope..

问题是当我们使用浏览器的多个选项卡时,它无法按预期工作.

The problem is it not working as expected when we are working with multiple tabs of the browser. .

我们有一个搜索页面,允许使用多个字段搜索数据库.我们使用@Sessionattributes将结果存储在会话中.

We have a search page that allows the use to search the database using multiple fields..We are storing the results in session using @Sessionattributes.

问题: 例如,用户提供一些输入并进行搜索,结果存储在会话中,名称为"searchresults".

Problem: For example user gives some input and searches and results are stored in session with name "searchresults".

如果用户打开新标签并再次使用其他条件进行搜索,则搜索结果将存储在会话中,名称为"searchresults".

If the user opens the new tab and searches using the different criteria again search results will be stored in session with name "searchresults"..

因此,如果用户重新加载第一个标签...他将在第二个标签中找到搜索结果.

So if user reloads the first tab...he will se search results whatever there in second tab..

So .. :: searchresults"将从第二个选项卡中获得结果...因此,即使用户刷新了第一个选项卡.他也将看到他从第二个选项卡中获得的结果.

So..:searchresults" in session will have results from second tab...so even if user refresh first tab..he will see results he got using the second tab..

这个春季m​​vc有什么解决方案吗?

Is there any solution for this spring mvc...

推荐答案

是的,如 lewthor 所述,多个标签共享一个会议.

Yeps, as lewthor says multiple tabs share a session.

处理这种情况的一种方法是使用特定于制表符的url组件,该组件将包含在会话密钥中.如果您在产品列表页面上,并且要为每个产品打开一个新标签,请确保打开标签时的网址不同,例如通过在URL /product/{product.id}中使用产品ID,您要做的所有正确的操作就是将ID附加到会话密钥searchresults{product.id}

One way to deal with the situation is to have a tab specific url component that will be included in the session key. If you are on a product listing page, and you're opening a new tab per product, if you make sure that the urls upon tab opening are different e.g. by using product id in the url /product/{product.id}, all you have to do make the proper behaviour is append the id to session key searchresults{product.id}

还有一个以@SessionAttribute为中心的解决方案,该服务仅用于此目的,在

There is also a @SessionAttribute centric solution, a customization that serves just this purpose, descibed in the blog here and based on the older blog described here. The solution implements a CustomSessionAttributeStore, and maintaines a Map of Maps where the inner Map is the deafult SessionAttributes, identified by the conversation id (in your case the tab id)

public class ConversationalSessionAttributeStore implements SessionAttributeStore, InitializingBean {

  @Inject
  private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
  private Logger logger = Logger.getLogger(ConversationalSessionAttributeStore.class.getName());

  private int keepAliveConversations = 10;

  public final static String CID_FIELD = "_cid";
  public final static String SESSION_MAP = "sessionConversationMap";

  @Override
  public void storeAttribute(WebRequest request, String attributeName, Object attributeValue) {
    Assert.notNull(request, "WebRequest must not be null");
    Assert.notNull(attributeName, "Attribute name must not be null");
    Assert.notNull(attributeValue, "Attribute value must not be null");

    String cId = getConversationId(request);
    if (cId == null || cId.trim().length() == 0) {
      cId = UUID.randomUUID().toString();
      request.setAttribute(CID_FIELD, cId, WebRequest.SCOPE_REQUEST);
    }

    logger.debug("storeAttribute - storing bean reference for (" + attributeName + ").");
    store(request, attributeName, attributeValue, cId);
  }

  private String getConversationId(WebRequest request) {
    return request.getParameter(CID_FIELD);
  }
}

整个项目发布在 GitHub

这篇关于Spring MVC @Sessionattributes问题在多个浏览器选项卡中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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