--- title: Customizing The Startup Page In An Application order: 25 layout: page --- [[customizing-the-startup-page-in-an-application]] = Customizing the startup page in an application In Vaadin 6, the startup page - used to bootstrap a new Vaadin UI instance in the browser - was generated as a monolithic chunk of HTML and was not easily customizable. In Vaadin 7, we added a new facility for registering special _bootstrap listeners_ that are invoked before the bootstrap response is sent. In addition, instead of bare HTML in a string, the response is now built as a DOM tree that is easy to manipulate programmatically. Here's an example of a simple bootstrap listener: [source,java] .... import org.jsoup.nodes.Comment; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import org.jsoup.parser.Tag; // ... new BootstrapListener() { @Override public void modifyBootstrapPage(BootstrapPageResponse response) { response.getDocument().body().appendChild(new Comment("Powered by Vaadin!", "")); } @Override public void modifyBootstrapFragment(BootstrapFragmentResponse response) { // Wrap the fragment in a custom div element Element myDiv = new Element(Tag.valueOf("div"), ""); List nodes = response.getFragmentNodes(); for(Node node : nodes) { myDiv.appendChild(node); } nodes.clear(); nodes.add(myDiv); } } .... The HTML library we use is http://jsoup.org/[jsoup]. It provides a very convenient API for traversing, manipulating and extracting data from a DOM, and is HTML5 compliant. The `BootstrapListener` interface contains two methods, one of which is usually left empty. This is because a Vaadin application can be either stand-alone, in which case it "owns" the whole page its UI resides in, or embedded, such as a portlet, in which case it does not control the content of the page it is embedded in. The `modifyBootstrapFragment` method is called in both cases. It receives a `BootstrapFragmentResponse` that represents the HTML fragment that is inserted in the host page, whether the page is controlled by Vaadin or not. Hence, you only need to implement this method if you do not care about the host page, whether your application is embedded or standalone. The `modifyBootstrapPage` method is called with a `BootstrapPageResponse` argument that represents the whole bootstrap page, including the fragment mentioned above. Thus, it is only invoked when the application is standalone and actually responsible for generating the page. This method allows you to, for instance, add things to the `head` element. The `BootstrapPageResponse` class also allows setting arbitrary HTTP response headers: [source,java] .... public void modifyBootstrapPage(BootstrapPageResponse response) { response.setHeader("X-Powered-By", "Vaadin 7"); } .... But how and where should the bootstrap listeners be registered? It should be only once per session, and right in the beginning, so that they are already added when the first response is sent. To do that you should write a custom servlet that extends `VaadinServlet`, or a custom portlet extending `VaadinPortlet`, and a session init listener that adds the bootstrap listener to the new session. [source,java] .... class MyVaadinServlet extends VaadinServlet { @Override protected void servletInitialized() throws ServletException { super.servletInitialized(); getService().addSessionInitListener(new SessionInitListener() { @Override public void sessionInit(SessionInitEvent event) { event.getSession().addBootstrapListener(listener); } }); } } // Or... class MyVaadinPortlet extends VaadinPortlet { @Override protected void portletInitialized() throws PortletException { super.portletInitialized(); getService().addSessionInitListener(new SessionInitListener() { @Override public void sessionInit(SessionInitEvent event) { event.getSession().addBootstrapListener(listener); } }); } } .... gration/issues_test.rb?id=d955672a7dc23da260836f13d43af87079b43b33'>issues_test.rb
blob: 84a1a6b6c6964e73d2e63707aa786f358d8c8512 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325