Vaadin 会话管理 - 它是如何工作的? [英] Vaadin Session Management - How does it work?

查看:94
本文介绍了Vaadin 会话管理 - 它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们使用 Vaadin 开发了一个 Web 应用程序.我是一名非常高级的 Java 程序员.我也有使用 Vaadin 的经验.但是现在我已经到了需要将信息存储在用户会话中的地步.诸如区域设置、用户名等属性.

At work, we develop a web application using Vaadin. I am a pretty advanced programmer in Java. I’m experienced with Vaadin as well. But now I've come to a point where information needs to be stored in a user session. Attributes like Locale, Username and so on.

Vaadin 文档中,他们讨论了两种不同的类型会话,但我并没有真正得到区别:

In the Vaadin Documentation they are talking about two different types of sessions but I dont really get the difference:

  • VaadinServletService or VaadinPortletService described as low-level customization layer for processing requests.
  • VaadinSession of a UI with getSession() as lower-level session objects.

有什么区别,当我想在整个 UI 独立用户会话期间存储属性时使用哪个?

What is the difference and which one is to use when I want so store attributes during the whole UI independent user-session?

推荐答案

如果在 Vaadin 8 中,您有简单的范围层次结构,分为三个级别.

If in Vaadin 8, you have simple hierarchy of scope, on three levels.

  • ServletContext
    代表你的整个 Vaadin 网络应用程序.
  • VaadinSession
    代表每个用户的工作会话.
  • UI
    代表会话中的每个网络浏览器/标签(Vaadin 支持多窗口应用,非常棒).

第一个是规范中定义的每个 Java Servlet 的标准部分.

The first is a standard part of every Java Servlet, defined in the spec.

第二个是会话的包装器,也在 Java Servlet 规范中定义.

The second is a wrapper around the session, also defined in the Java Servlet spec.

Vaadin 实际上是一个巨大的 Servlet,所以它承载了 context 和 session 的这些特性.

Vaadin is actually one huge Servlet, so it carries these features of context and session.

UI 类是 Vaadin 独有的.它代表 Web 浏览器窗口/选项卡的内容.Vaadin 支持多窗口应用程序,跟踪所有打开的窗口/标签作为会话的一部分,这是 Vaadin 的一个非常好的功能.

The UI class is unique to Vaadin. It represents the content of a web browser window/tab. Vaadin supports multi-window apps, tracking all the open windows/tabs as part of the session, a very nice feature of Vaadin.

要存储全州应用程序,请使用标准的 ServletContext 对象.它带有一个称为属性"的键值集合.键为String类型,值为Object 类型.调用方法 setAttributegetAttributeremoveAttribute.将此集合用于您可能需要为任何用户访问的任何对象,访问他们的会话.你可以了解ServletContextListener 顺便说一下,挂钩到您的网络应用程序启动和退出.

To store state-wide app, use the standard ServletContext object. It carries a key-value collection known as "attributes". The key is of String type, and the value is of Object type. Call methods setAttribute, getAttribute, and removeAttribute. Use this collection for any objects you may need to access for any of your users, accoss their sessions. You may to learn about ServletContextListener by the way, to hook into your web app launching and exiting.

VaadinSession 类携带相同类型的键值集合,具有类似的属性"方法.使用它来跟踪整个用户工作会话中的项目,可能会打开/关闭应用程序的多个窗口/选项卡.例如,在会话中,您将存储用户的 Spirit Animal 选择、他们的头像以及此人已通过用户名/密码凭据进行身份验证的事实.请参阅手册中的此页面,设置和阅读会话属性.

The VaadinSession class carries the same kind of key-value collection, with similar "attribute" methods. Use this to track items throughout the user’s work session, across them possibly opening/colsing multiple windows/tabs of your app. For example, in the session you would store the user’s Spirit Animal choice, their avatar image, and the fact that person has been authenticated via username/password credentials. See this page in the manual, Setting and reading session attributes.

如果您想存储每个窗口的设置,例如他们选择的背景颜色或亮/暗模式,请在 UI 上存储一些内容.不幸的是,该类没有我所知道的方便的键值存储.因此,您需要将自己的 Map 或其他一些成员变量添加到您的 UI 子类中.

If you want to store per-window settings, for something like their choice of background color or light/dark mode, store something on UI. Unfortunately, that class does not come with a convenient key-value store that I know of. So you'll need to add your own Map, or some other member variables to your UI subclass.

有关详细信息,请参阅对类似问题的我的回答.我在那里做了一些漂亮的图表.这个问题是关于 Vaadin 7,但我依稀记得,这些概念在 7 和 8 之间延续.

For more info, see my Answer to a similar Question. I made some nifty diagrams there. That question is about Vaadin 7, but as I vaguely recall, these concepts carry over between 7 and 8.

至于 VaadinServiceVaadinServletService,我从来没有完全理解它们的作用.它们似乎在运行时代表了整个 Vaadin 网络的各个方面.但它们都不像 VaadinSession 那样携带方便的键值集合.(如果您使用 Vaadin Flow,版本 10+,请参阅 VaadinContext,该类代表您的整个 Web 应用程序,并携带方便的属性"键值集合.)

As for VaadinService and VaadinServletService, I have never understood exactly their role. They seem to represent various aspects of your entire Vaadin web at runtime. But neither carries a convenient key-value collection as like VaadinSession. (If you use Vaadin Flow, versions 10+, see VaadinContext, a class than does represent your entire web app, and does carry a convenient key-value collection of "attributes".)

这篇关于Vaadin 会话管理 - 它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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