如何将数据放入会话变量并在vaadin的不同页面中获取数据? [英] how to put data in session variable and get the data in different page in vaadin?

查看:15
本文介绍了如何将数据放入会话变量并在vaadin的不同页面中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我应该使用应用程序范围会话来处理这个问题.但我对此没有任何经验.我尝试了从互联网上获得的不同方法,例如:

HttpServletRequest 请求;HttpSession sess = request.getSession();sess.setAttribute("name", name);稍后在其他页面HttpServletRequest 请求;String=(String)request.getAttribute(name);//或 HttpSession sess = request.getSession();//sess.getAttribute(name);

都不起作用.我认为 vaadin 可能有一些特别的东西来处理这个问题.请帮我.

解决方案

两级范围

参见

多个窗口

Vaadin 7 支持在同一个 Vaadin 应用程序上打开多个浏览器窗口/选项卡.这是 Vaadin 6 架构的重大变化.

每个浏览器窗口/选项卡的内容是您的 UI 子类的一个实例.所有这些实例都属于同一个 VaadinSession.如果用户单击浏览器窗口/选项卡上的重新加载功能,UI 实例将被销毁并实例化一个新实例.但 VaadinSession 经久不衰.添加 @PreserveOnRefresh 注释保留相同 UI 实例的行为,但这不是重点.

重点是 Vaadin 7 有两个级别的范围:

  • VaadinSession(你的整个应用)
  • UI(每个浏览器窗口/标签).

您可能希望在任一级别存储数据.例如,用户登录/身份验证信息应包含在 VaadinSession 中.

将状态放在 UI

要将数据存储在 UI 上,请将字段或集合添加到您的 UI 子类.简单明了.

将状态置于 VaadinSession

要在 VaadinSession 上存储数据,请调用 setAttributegetAttribute.你将不得不投射结果.简单,除了一个问题:线程安全.

线程安全

您可以手动管理锁定以同时保护 VaadinSession.但是,如果您遵循记录的规则,Vaadin 将为您提供线程安全保护.

主线程

如果从通常的主用户界面线程修改 VaadinSession,没有问题.您在 Vaadin 中从主线程执行的大部分操作都已经影响了 VaadinSession 对象.这就是您的应用程序所在的位置.所以线程安全锁已经由 Vaadin 自动提供了.

其他主题

如果使用来自另一个线程的 VaadinSession,请在 Runnable 传递给 UI 或 VaadinSession 对象上的 access 方法.如果除了会话影响任何用户界面布局或小部件,请调用 access 方法.如果仅影响会话而不影响用户界面,请将您的 Runnable 传递给 VaadinSession 上的access 方法.

第三级范围:应用范围

仅供参考,您可以使用更广泛的范围.如果您有全局"数据或对象要在所有用户的会话中共享应用程序范围,您可以访问 ServletContext 对象.上下文"意味着您的网络应用程序的世界,您的网络应用程序可以访问的内容.您的 Vaadin 应用程序有一个由您的 Servlet 容器自动实例化的 ServletContext 对象.调用这对方法,getAttributesetAttribute,用于存储任何Object 你有.

有关更多信息,请参阅此问题 &我的回答,如何从 Vaadin 7 应用程序中访问 ServletContext?.

用文字(和下图)总结一下:Tomcat 或 Jetty 等 Servlet 容器可以运行一个或多个 Vaadin Web 应用程序.每个 Web 应用程序都有一个由容器自动管理的 ServletContext 对象,其中 get/setAttribute 方法可以存储您选择的任何对象.每个 Vaadin 网络应用程序都有一个 VaadinSession 对象用于每个用户的当前工作会话.每个 VaadinSession 对象包含一个或多个 UI 子类实例,代表在 Web 浏览器的窗口/选项卡(或 Portlet 视图区域)中看到的内容.每个 UI 子类实例可以有任意数量的成员变量,就像任何 POJO.

<小时>

提示:如果需要,您的 Runnable 可以使用 Java 8 中新的更短的 Lambda 语法.NetBeans 8 甚至会建议这样做.

I think I should use the application scope session to deal with that. But I do not have any experience about that. I tried different ways I got from the internet like:

HttpServletRequest request;
HttpSession sess = request.getSession();
sess.setAttribute("name", name);

later in other page
HttpServletRequest request;
String=(String)request.getAttribute(name); 
//or HttpSession sess = request.getSession();
// sess.getAttribute(name);

all do not work. I think there may something special for vaadin to deal with that. Please help me.

解决方案

Two Levels Of Scope

See this posting by Roland Krüger about level of scope in Vaadin 7 with VaadinSession and UI classes. He includes a comparison to Vaadin 6. Good article except that the discussion of manual locking is outmoded as of Vaadin 7.1 (see my comment on that page, and see my answer to a similar question).

Understand that while the bulk of Vaadin 6 & Vaadin 7 are similar or the same, on this topic 6 & 7 are entirely different, with different architecture and different classes involved.

Note that we are discussing VaadinSession, not the HTTP or Servlet session. VaadinSession wraps or contains the Servlet session, so we need not concern ourselves with the Servlet level.

This VaadinSession represents the work-session of a single user. A user has one or more windows/tabs open (UI instances).

That diagram above is a bit over-simplified. Here's a more detailed one.

Multiple Windows

Vaadin 7 supports multiple browser windows/tabs open on the same Vaadin app. This is a major change in architecture from Vaadin 6.

Content of each browser window/tab is an instance of your UI subclass. All of those instances belong to the same VaadinSession. If the user clicks the reload feature on the browser window/tab, the UI instance is destroyed and a new one instantiated. But the VaadinSession endures. Adding the @PreserveOnRefresh annotation changes that behavior to retain the same UI instance, but that is beside the point.

The point is that Vaadin 7 has two levels of scope:

  • VaadinSession (your whole app)
  • UI (each browser window/tab).

You may want to store data at either level. For example, user login/authentication info should go in the VaadinSession.

Putting State on UI

To store data on the UI, add fields or collections to your UI subclass. Simple and obvious.

Putting State On VaadinSession

To store data on the VaadinSession, call setAttribute and getAttribute. You will have to cast the results. Simple, except for one catch: thread-safety.

Thread-Safety

You can manually mange locking to protect the VaadinSession concurrently. But Vaadin will do provide that thread-safety protection for you if you follow the documented rules.

Main Thread

If modifying the VaadinSession from the usual main user interface thread, no issue. Most everything you do in Vaadin from the main thread is already affecting the VaadinSession object. That's where your app lives. So thread-safe locking is already automatically provided by Vaadin.

Other Threads

If using the VaadinSession from another thread, make your call in a Runnable you pass to the access method on either the UI or the VaadinSession object. If affecting any user interface layout or widgets in addition to the session, call the access method on the UI method. If affecting only the session and not the user interface, pass your Runnable the access method on the VaadinSession.

Third Level of Scope: App-Wide

FYI, you can use a wider scope. If you have "global" data or objects to share app-wide across all the users’ sessions, you can access the ServletContext object. "Context" means the world of your web app, the stuff your web app can access. Your Vaadin app has a single ServletContext object automatically instantiated by your Servlet container. Call the pair of methods, getAttribute and setAttribute, to store any Object you have.

For more information, see this Question & Answer of mine, How to access ServletContext from within a Vaadin 7 app?.

To sum it up in text (and diagram below): A Servlet container such as Tomcat or Jetty can run one or more Vaadin web apps. Each web app has a single ServletContext object automatically managed by the container, where get/setAttribute methods can store any Objects of your choice. Each Vaadin web app has one VaadinSession object for each user’s current work-session. Each VaadinSession object contains one or more UI subclass instances, representing the content seen within a web browser’s window/tab (or a Portlet view area). Each UI subclass instance can have any number of member variables, like any POJO.


Tip: your Runnable can use the new shorter Lambda syntax in Java 8 if you want. NetBeans 8 will even suggest that.

这篇关于如何将数据放入会话变量并在vaadin的不同页面中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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