Vaadin 14应用程序中将"UI"对象作用域状态存储在哪里? [英] Where to store `UI`-object scoped state in a Vaadin 14 app?

查看:73
本文介绍了Vaadin 14应用程序中将"UI"对象作用域状态存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要存储可用于我们整个Vaadin应用程序的状态,我们可以在

To store state available to our entire Vaadin app, we can get and set "Attribute" on the VaadinContext object that represents our entire Vaadin-based web app at runtime. These attributes act as a key-value collection, where the key is of type String and the value is of type Object.

我们通过调用UI.getCurrent().getSession().getService().getContext()来访问上下文.

We access the context by calling UI.getCurrent().getSession().getService().getContext().

要存储任何用户会话可用的状态,我们可以类似地在

To store state available to any one user’s session, we can similarly get and set "attributes" on the VaadinSession object.

我们通过调用UI.getCurrent().getSession()来访问会话.

We access the session by calling UI.getCurrent().getSession().

这两个范围级别,即上下文和会话是围绕 Java Servlet 规范中定义的等效项的包装.但Vaadin实际上具有第三,更好的范围. Vaadin支持多窗口应用程序,其中每个Web浏览器窗口(或选项卡)都有自己的内容,这些内容由

These two levels of scope, context & session, are wrappers around their equivalents defined in the Java Servlet specification. But Vaadin effectively has a third, finer level of scope. Vaadin supports multi-window apps, where each web browser window (or tab) has its own content handled by a UI object. If a user has three windows open within our Vaadin app, that user has three UI object instances on the server housed within a single VaadinSession object.

因此,似乎最常见的需求是每个UI(网络浏览器窗口/标签)存储状态.因此,我希望看到同一种getAttribute&如VaadinSession&中所见,UI上的setAttribute方法. VaadinContext.但是,不,我在UI上没有看到这样的方法.

So it seems like a common need would be storing state per UI (web browser window/tab). So I would expect to see the same kind of getAttribute & setAttribute methods on UI as seen on VaadinSession & VaadinContext. But, no, I do not see such methods on UI.

➥是否有适当的位置存储每个UI对象的状态?

➥ Is there an appropriate place to store state per UI object?

在过去的Vaadin中,我们总是编写自己的UI子类.因此,我们总是可以通过在自己的UI-子类上定义成员变量来存储状态.现在,在Vaadin Flow(v10 +,目前为14)时代,我们不鼓励(禁止?)编写UI的子类.

In the olden days, in previous generations of Vaadin, we always wrote our own subclass of UI. So we could always store state by defining member variables on our own UI-subclass. Now, in the days of Vaadin Flow (v10+, currently 14), we are discouraged (forbidden?) from writing a subclass of UI.

在对这些属性进行功能请求之前,我想问一下我是否错过了人们通常在基于Vaadin的应用程序中存储其per UI状态的常规位置.

Before filing a feature-request for such attributes, I want to ask if I missed out on a usual place where folks store their per-UI state in current Vaadin-based apps.

推荐答案

在Vaadin Flow中,有

In Vaadin Flow there is ComponentUtil helper class which has methods to store data with components and UI.

请参阅一对ComponentUtil.setData方法,一个

See the pair of ComponentUtil.setData methods, one taking a Class as key, the other taking a String as key, just like the getAttribute/setAttribute methods found on VaadinContext & VaadinSession.

这篇关于Vaadin 14应用程序中将"UI"对象作用域状态存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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