如何将数据放在会话变量中,并获取vaadin中不同页面中的数据? [英] how to put data in session variable and get the data in different page in vaadin?
问题描述
我想我应该使用应用程序范围会话来处理。但我没有任何经验。我尝试了不同的方式,我从互联网,如:
HttpServletRequest请求;
HttpSession sess = request.getSession();
sess.setAttribute(name,name);
稍后在其他页面
HttpServletRequest请求;
String =(String)request.getAttribute(name);
//或HttpSession sess = request.getSession();
// sess.getAttribute(name);
都不起作用。我认为可能有一些特别的vaadin来处理。请帮帮我。
两个范围
请参阅
多个Windows
Vaadin 7支持在同一个Vaadin应用程序上打开多个浏览器窗口/标签。这是Vaadin 6的一个重大的架构变化。
每个浏览器窗口/标签的内容是 UI
子类。所有这些实例属于相同的 VaadinSession
。如果用户在浏览器窗口/选项卡上单击重新载入功能,则会破坏 UI
实例,并实例化一个新实例。但 VaadinSession
忍受。添加 @PreserveOnRefresh
注释更改该行为以保留相同的UI实例,但这不是问题。
重要的是,Vaadin 7有两个级别的范围:
-
VaadinSession
(您的整个应用程式) -
UI
。
您可能要在任一级别存储数据。例如,用户登录/身份验证信息应该在VaadinSession中。
将状态置于 UI
要在UI上存储数据,请将字段或集合添加到UI子类中。简单明了。
将状态置于 VaadinSession
要在VaadinSession上存储数据,请调用 setAttribute
和 getAttribute
。您将必须投射结果。简单,除了一个catch:thread-safety。
线程安全
您可以手动管理锁定以同时保护VaadinSession。但是,如果你遵循文件化的规则,Vaadin会为你提供线程安全保护。
主线程
如果从通常的主用户界面线程修改VaadinSession,没有问题。你在Vaadin中从主线程做的大多数事情都已经影响着VaadinSession对象。这就是你的应用程序生活。
其他主题
如果使用另一个VaadinSession线程,请在 Runnable 中拨打电话您传递到UI或VaadinSession对象上的访问
方法。如果影响除会话之外的任何用户界面布局或窗口小部件,请调用 访问
方法。如果只影响会话而不是用户界面,请将您的Runnable传递给 访问
方法。
第三级范围:应用程式范围
FYI,您可以使用更广泛的范围。如果您有全球数据或对象在所有用户的会话中共享应用范围,您可以访问 ServletContext
对象。 上下文是指您的网络应用程序的世界,您的网络应用程序可以访问的东西。你的Vaadin应用程序有一个单独的 ServletContext
对象,由Servlet容器自动实例化。调用这对方法, getAttribute
和 setAttribute
,以存储任何 对象
你有。
有关详细信息,请参阅此问题&我的答案,如何从Vaadin 7应用程序中访问 ServletContext
?< a>。
在文本(和下图)中总结:一个Servlet容器,如Tomcat或Jetty可以运行一个或多个Vaadin web应用程序。每个Web应用程序都有一个由容器自动管理的 ServletContext
对象,其中 get / setAttribute
方法可以存储任何对象你的选择。每个Vaadin网络应用程序对每个用户当前的工作会话有一个 VaadinSession
对象。每个 VaadinSession
对象包含一个或多个 UI
子类实例,表示在浏览器的窗口/ Portlet视图区域)。每个 UI
子类实例可以有任意数量的成员变量,例如 POJO 。
提示:Runnable可以使用新的更短的Lambda语法在Java 8如果你想要。 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屋!