问题描述
在使用Vaadin Flow版本14时,我知道我们可以通过在以下位置找到的getAttribute
/setAttribute
/removeAttribute
方法将状态存储为键值对,并保留为属性".
-
VaadinSession
(按用户范围) -
VaadinContext
(网络应用范围)
如何访问那些类的当前对象?
VaadinSession.getCurrent()
➙VaadinSession
对于该每个用户范围, VaadinSesion
类提供了静态类方法 getCurrent
来访问当前实例.
VaadinSession session = VaadinSession.getCurrent() ; // Fetch current instance of `VaadinSession` to use its key-value collection of attributes.
session.setAttribute( User.class , user ) ; // Register user's successful authentication.
VaadinService.getCurrent().getContext()
➙VaadinContext
对于该网络应用范围的范围,您必须跳过一个额外的范围.实际上 VaadinService
类代表整个网络应用.但是它将属性功能委托给 VaadinContext
类,该类的一个实例由当前服务实例跟踪.因此获取服务,并将其用于获取上下文.
VaadinContext context = VaadinService.getCurrent().getContext() ; // Get the current `VaadinService` object, and ask it for the current `VaadinSession` object.
context.setAttribute( ServiceLocator.class , new ServiceLocatorForTesting() ) ;
VaadinServlet.getCurrent().getServletContext()
➙ServletContext
上面讨论的VaadinContext
对象确实提供了Web应用程序范围的范围,用于将对象另存为键-值映射中的属性".但是,请注意,键必须为Class
.有时String
键可能会更好.
如果要在整个Web应用程序中使用基于字符串的键值映射,请使用标准的 ServletContext
.此接口在 Jakarta Servlet 标准中定义. setAttribute
, getAttribute
, removeAttribute
和 getAttributeNames()
方法都使用String
作为键,并使用Object
作为值.
ServletContext servletContext = VaadinServlet.getCurrent().getServletContext() ;
将对象存储为属性.
servletContext.setAttribute( "map_of_department_to_manager" , map ) ;
由于该值未使用 Java泛型,因此在访问存储的值时必须进行强制转换
Map< Department , Manager > map =
( Map< Department , Manager > ) // Casting from `Object`.
servletContext.getAttribute( "map_of_department_to_manager" )
;
如果确实只有一个特定类的对象要存储,则可以将类名用作基于字符串的键.
servletContext.setAttribute(
ServiceLocator.class.getCanonicalName() ,
new ServiceLocatorForTesting()
) ;
检索.
ServiceLocator serviceLocator =
( ServiceLocator ) // Must cast the retrieved object.
servletContext.getAttribute(
ServiceLocator.class.getCanonicalName() // Using name of class as our `String` key.
)
;
In using Vaadin Flow, version 14, I know we can store state as key-value pairs kept as "attributes" via getAttribute
/setAttribute
/removeAttribute
methods found on:
VaadinSession
(per-user scope)VaadinContext
(web-app scope)
How does one access the current object for those classes?
VaadinSession.getCurrent()
➙ VaadinSession
For that per-user scope, the VaadinSesion
class provides a static class method getCurrent
to access the current instance.
VaadinSession session = VaadinSession.getCurrent() ; // Fetch current instance of `VaadinSession` to use its key-value collection of attributes.
session.setAttribute( User.class , user ) ; // Register user's successful authentication.
VaadinService.getCurrent().getContext()
➙ VaadinContext
For that web-app-wide scope, you must jump through one extra hoop. The VaadinService
class actually represents the web app as a whole. But it delegates the attributes feature to the VaadinContext
class, an instance of which is tracked by the current service instance. So get the service, and use that to get the context.
VaadinContext context = VaadinService.getCurrent().getContext() ; // Get the current `VaadinService` object, and ask it for the current `VaadinSession` object.
context.setAttribute( ServiceLocator.class , new ServiceLocatorForTesting() ) ;
VaadinServlet.getCurrent().getServletContext()
➙ ServletContext
The VaadinContext
object discussed above does provide web-app-wide scope for saving objects as "attributes" in a key-value mapping. However, notice that the key to must be a Class
. Sometimes a String
key might work better.
If you want a String-based key-value mapping across your web app, use the standard ServletContext
. This interface is defined in the Jakarta Servlet standard. The setAttribute
, getAttribute
, removeAttribute
, and getAttributeNames()
methods all use String
as the key, and Object
as the value.
ServletContext servletContext = VaadinServlet.getCurrent().getServletContext() ;
Store your object as an attribute.
servletContext.setAttribute( "map_of_department_to_manager" , map ) ;
Since the value does not use Java Generics, we must cast when accessing a stored value.
Map< Department , Manager > map =
( Map< Department , Manager > ) // Casting from `Object`.
servletContext.getAttribute( "map_of_department_to_manager" )
;
If you do have only a single object of a particular class to store, you can use the class name as the string-based key.
servletContext.setAttribute(
ServiceLocator.class.getCanonicalName() ,
new ServiceLocatorForTesting()
) ;
Retrieval.
ServiceLocator serviceLocator =
( ServiceLocator ) // Must cast the retrieved object.
servletContext.getAttribute(
ServiceLocator.class.getCanonicalName() // Using name of class as our `String` key.
)
;
这篇关于在Vaadin Flow中获取当前的VaadinContext和当前的VaadinSession(将状态存储为“属性"键/值对的两个地方)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!