blob: 72db6bef110a6d403e60d1bf5afa65ad0b001954 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
---
title: Setting And Reading Session Attributes
order: 9
layout: page
---
[[setting-and-reading-session-attributes]]
Setting and reading session attributes
--------------------------------------
Vaadin has a few different ways of storing data that should be
accessible later. Which one you should use depends on the scope of the
data, e.g. how long it should be kept around and what parts of the code
should have access to it.
1. Store the data as a field in your UI subclass. The data is easily
accessible from components belonging to that UI instance and the data
will be gone when the UI is closed.
2. Store data in the `VaadinServiceSession`. The data is easily
accessible from any UI belonging to the same VaadinServlet or
VaadinPortlet and it will be gone when the session is closed.
3. Store data in the `HttpSession` or `PortletSession` (represented in
Vaadin through `WrappedSession`). The data is easily accessible from any
part of your web application (i.e. your .war) and it will be gone when
the session is invalidated.
The following example code demonstrates how the data is stored and
retrieved in different ways.
[source,java]
....
//Remove comment to preserve UI value when reloading
//@PreserveOnRefresh
public class SettingReadingSessionAttributesUI extends UI {
private String value;
private VerticalLayout statusHolder = new VerticalLayout();
private TextField textField = new TextField();
@Override
protected void init(VaadinRequest request) {
addComponent(statusHolder);
addComponent(textField);
addComponent(new Button("Set new values", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
String value = textField.getValue();
saveValue(SettingReadingSessionAttributesUI.this, value);
}
}));
addComponent(new Button("Reload page", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
getPage().setLocation(getPage().getLocation());
}
}));
showValue(this);
}
private static void saveValue(SettingReadingSessionAttributesUI ui,
String value) {
// Save to UI instance
ui.value = value;
// Save to VaadinServiceSession
ui.getSession().setAttribute("myValue", value);
// Save to HttpSession
VaadinService.getCurrentRequest().getWrappedSession()
.setAttribute("myValue", value);
// Show new values
showValue(ui);
}
private static void showValue(SettingReadingSessionAttributesUI ui) {
ui.statusHolder.removeAllComponents();
ui.statusHolder.addComponent(new Label("Value in UI: " + ui.value));
ui.statusHolder.addComponent(new Label(
"Value in VaadinServiceSession: "
+ ui.getSession().getAttribute("myValue")));
ui.statusHolder.addComponent(new Label("Value in HttpSession: "
+ VaadinService.getCurrentRequest().getWrappedSession()
.getAttribute("myValue")));
}
}
....
The UI stores and reads the value in three different ways. The UI
instance value gets lost just by reloading the page as a new UI instance
is then created unless the UI has been marked with `@PreserveOnRefresh`.
If you deploy two different `VaadinServlet` instances using the same UI
class, they will only share the `HttpSession` value.
|