You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-embedding.asciidoc 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. ---
  2. title: Embedding UIs in Web Pages
  3. order: 2
  4. layout: page
  5. ---
  6. [[advanced.embedding]]
  7. = Embedding UIs in Web Pages
  8. Many web sites are not all Vaadin, but Vaadin UIs are used only for specific
  9. functionalities. In practice, many web applications are a mixture of dynamic web
  10. pages, such as JSP, and Vaadin UIs embedded in such pages.
  11. Embedding Vaadin UIs in web pages is easy and there are several different ways
  12. to embed them. One is to have a [literal]#++<div>++# placeholder for the UI and
  13. load the Vaadin Client-Side Engine with some simple JavaScript code. Another
  14. method is even easier, which is to simply use the [literal]#++<iframe>++#
  15. element. Both of these methods have advantages and disadvantages. One
  16. disadvantage of the [literal]#++<iframe>++# method is that the size of the
  17. [literal]#++<iframe>++# element is not flexible according to the content while
  18. the [literal]#++<div>++# method allows such flexibility. The following sections
  19. look closer into these two embedding methods.
  20. [[advanced.embedding.div]]
  21. == Embedding Inside a [literal]#++div++# Element
  22. You can embed one or more Vaadin UIs inside a web page with a method that is
  23. equivalent to loading the initial page content from the Vaadin servlet in a
  24. non-embedded UI. Normally, the [classname]#VaadinServlet# generates an initial
  25. page that contains the correct parameters for the specific UI. You can easily
  26. configure it to load multiple Vaadin UIs in the same page. They can have
  27. different widget sets and different themes.
  28. Embedding an UI requires the following basic tasks:
  29. * Set up the page header
  30. * Call the [filename]#vaadinBootstrap.js# file
  31. * Define the [literal]#++<div>++# element for the UI
  32. * Configure and initialize the UI
  33. Notice that you can view the loader page for the UI easily by opening the UI in
  34. a web browser and viewing the HTML source code of the page. You could just copy
  35. and paste the embedding code from the page, but some modifications and
  36. additional settings are required, mainly related to the URLs that have to be
  37. made relative to the page instead of the servlet URL.
  38. ifdef::web[]
  39. [[advanced.embedding.div.head]]
  40. === The Head Matter
  41. The HTML page in which the Vaadin UI is embedded should be a valid HTML 5
  42. document. The content of the head element is largely up to you. The character
  43. encoding must be UTF-8. Some meta declarations are necessary for compatibility.
  44. You can also set the page favicon in the head element.
  45. [subs="normal"]
  46. ----
  47. &lt;!DOCTYPE html&gt;
  48. &lt;html&gt;
  49. &lt;head&gt;
  50. &lt;meta http-equiv="Content-Type"
  51. content="text/html; charset=UTF-8" /&gt;
  52. &lt;meta http-equiv="X-UA-Compatible"
  53. content="IE=9;chrome=1" /&gt;
  54. &lt;title&gt;[replaceable]#This is my Embedding Page#&lt;/title&gt;
  55. &lt;!-- Set up the favicon from the Vaadin theme --&gt;
  56. &lt;link rel="shortcut icon" type="image/vnd.microsoft.icon"
  57. href="/VAADIN/themes/[replaceable]#reindeer#/favicon.ico" /&gt;
  58. &lt;link rel="icon" type="image/vnd.microsoft.icon"
  59. href="/VAADIN/themes/[replaceable]#reindeer#/favicon.ico" /&gt;
  60. &lt;/head&gt;
  61. ----
  62. endif::web[]
  63. ifdef::web[]
  64. [[advanced.embedding.div.body]]
  65. === The Body Matter
  66. The page content must include some Vaadin-related definitions before you can
  67. embed Vaadin UIs in it.
  68. The [filename]#vaadinBootstrap.js# script makes definitions for starting up the
  69. UI. It must be called before initializing the UI. The source path must be
  70. relative to the path of the embedding page.
  71. [subs="normal"]
  72. ----
  73. &lt;body&gt;
  74. &lt;script type="text/javascript"
  75. src="[replaceable]#./#VAADIN/vaadinBootstrap.js"&gt;&lt;/script&gt;
  76. ----
  77. The bootstrap script is served by the Vaadin servlet from inside the
  78. [filename]#vaadin-server# JAR.
  79. endif::web[]
  80. ifdef::web[]
  81. [[advanced.embedding.div.div]]
  82. === UI Placeholder Element
  83. A Vaadin UI is embedded in a placeholder [literal]#++<div>++# element. It should
  84. have the following features:
  85. * The [literal]#++<div>++# element must have an [literal]#++id++# attribute, which must be a unique ID in the page, normally something that identifies the servlet of the UI uniquely.
  86. * It must have at least the [literal]#++v-app++# style class.
  87. * it should have a nested [literal]#++<div>++# element with [literal]#++v-app-loading++# style class. This is a placeholder for the loading indicator that is displayed while the UI is being loaded.
  88. * It should also contain a [literal]#++<noscript>++# element that is shown if the browser does not support JavaScript or it has been disabled. The content of the element should instruct the user to enable JavaScript in the browser.
  89. The placeholder element can include style settings, typically a width and
  90. height. If the sizes are not defined, the UI will have an undefined size in the
  91. particular dimension, which must be in accordance with the sizing of the UI
  92. components.
  93. For example:
  94. [subs="normal"]
  95. ----
  96. &lt;div style="[replaceable]#width: 300px; border: 2px solid green;#"
  97. id="helloworldui" class="v-app"&gt;
  98. &lt;div class="v-app-loading"&gt;&lt;/div&gt;
  99. &lt;noscript&gt;[replaceable]#You have to enable javascript in your browser to#
  100. [replaceable]#use an application built with Vaadin.#&lt;/noscript&gt;
  101. &lt;/div&gt;
  102. ----
  103. endif::web[]
  104. ifdef::web[]
  105. [[advanced.embedding.div.init]]
  106. === Initializing the UI
  107. The UI is loaded by calling the [literal]#++initApplication()++# method for the
  108. [literal]#++vaadin++# object defined in the bootstrap script. Before calling it,
  109. you should check that the bootstrap script was loaded properly.
  110. [subs="normal"]
  111. ----
  112. &lt;script type="text/javascript"&gt;//&lt;!\[CDATA[
  113. if (!window.vaadin)
  114. alert("[replaceable]#Failed to load the bootstrap JavaScript:#"+
  115. "[replaceable]#VAADIN/vaadinBootstrap.js#");
  116. ----
  117. The [literal]#++initApplication()++# takes two parameters. The first parameter
  118. is the UI identifier, exactly as given as the [literal]#++id++# attribute of the
  119. placeholder element. The second parameter is an associative map that contains
  120. parameters for the UI.
  121. The map must contain the following items:
  122. [parameter]#browserDetailsUrl#:: This should be the URL path (relative to the embedding page) to the Vaadin
  123. servlet of the UI. It is used by the bootstrap to communicate browser details. A
  124. trailing slash may be needed in some cases.
  125. +
  126. Notice that this parameter is not included in the loader page generated by the
  127. servlet, because in that case, it can default to the current URL.
  128. [parameter]#serviceUrl#:: This is used for server requests after initial loading and should be same as for
  129. [parameter]#browserDetailsUrl#. The two parameters are redundant and either may
  130. be removed in
  131. future.+
  132. //Bug
  133. #10122
  134. [parameter]#widgetset#:: This should be the exact class name of the widget set for the UI, that is, without the [filename]#.gwt.xml# file name extension. If the UI has no custom widget set, you can use the [classname]#com.vaadin.DefaultWidgetSet#.
  135. [parameter]#theme#:: Name of the theme, such as one of the built-in themes ( [literal]#++reindeer++#, [literal]#++runo++#, or [literal]#++chameleon++#) or a custom theme. It must exist under the [filename]#VAADIN/themes# folder.
  136. [parameter]#versionInfo#:: This parameter is itself an associative map that can contain two parameters: [parameter]#vaadinVersion# contains the version number of the Vaadin version used by the application. The [parameter]#applicationVersion# parameter contains the version of the particular application. The contained parameters are optional, but the [parameter]#versionInfo# parameter itself is not.
  137. [parameter]#vaadinDir#:: Relative path to the [filename]#VAADIN# directory. It is relative to the URL of the embedding page.
  138. [parameter]#heartbeatInterval#:: The [parameter]#hearbeatInterval# parameter defines the frequency of the keep-alive hearbeat for the UI in seconds, as described in <<../application/application-lifecycle#application.lifecycle.ui-expiration,"UI Expiration">>.
  139. [parameter]#debug#:: The parameter defines whether the debug window, as described in <<../advanced/advanced-debug#advanced.debug,"Debug Mode and Window">>, is enabled.
  140. [parameter]#standalone#:: This parameter should be [parameter]#false# when embedding. The parameter defines whether the UI is rendered on its own in the browser window or in some context. A standalone UI may do things that might interfere with other parts of the page, such as change the page title and request focus when it is loaded. When embedding, the UI is not standalone.
  141. [parameter]#authErrMsg#,[parameter]#comErrMsg#, and[parameter]#sessExpMsg#:: These three parameters define the client-side error messages for authentication error, communication error, and session expiration, respectively. The parameters are associative maps themselves and must contain two key-value pairs: [parameter]#message#, which should contain the error text in HTML, and [parameter]#caption#, which should be the error caption.
  142. For example:
  143. [subs="normal"]
  144. ----
  145. vaadin.initApplication("[replaceable]#helloworldui#", {
  146. "browserDetailsUrl": "[replaceable]#helloworld#/",
  147. "serviceUrl": "[replaceable]#helloworld#/",
  148. "widgetset": "[replaceable]#com.example.MyWidgetSet#",
  149. "theme": "[replaceable]#mytheme#",
  150. "versionInfo": {"vaadinVersion": "[replaceable]#8.1.6#"},
  151. "vaadinDir": "[replaceable]#VAADIN/#",
  152. "heartbeatInterval": [replaceable]#300#,
  153. "debug": [replaceable]#true#,
  154. "standalone": false,
  155. "authErrMsg": {
  156. "message": "[replaceable]#Take note of any unsaved data, "+ "and &lt;u&gt;click here&lt;\/u&gt; to continue.#",
  157. "caption": "Authentication problem"
  158. },
  159. "comErrMsg": {
  160. "message": "[replaceable]#Take note of any unsaved data, "+ "and &lt;u&gt;click here&lt;\/u&gt; to continue.#",
  161. "caption": "Communication problem"
  162. },
  163. "sessExpMsg": {
  164. "message": "[replaceable]#Take note of any unsaved data, "+ "and &lt;u&gt;click here&lt;\/u&gt; to continue.#",
  165. "caption": "Session Expired"
  166. }
  167. });//]]&gt;
  168. &lt;/script&gt;
  169. ----
  170. Notice that many of the parameters are normally deployment parameters, specified
  171. in the deployment descriptor, as described in
  172. <<../application/application-environment#application.environment.parameters,"Other
  173. Servlet Configuration Parameters">>.
  174. endif::web[]
  175. ifdef::web[]
  176. [[advanced.embedding.div.summary]]
  177. === Summary of Div Embedding
  178. Below is a complete example of embedding an UI in a [literal]#++<div>++#
  179. element.
  180. [source, html]
  181. ----
  182. <?xml version="1.0" encoding="UTF-8" ?>
  183. <!DOCTYPE html>
  184. <html>
  185. <head>
  186. <meta http-equiv="Content-Type"
  187. content="text/html; charset=UTF-8" />
  188. <meta http-equiv="X-UA-Compatible"
  189. content="IE=9;chrome=1" />
  190. <title>Embedding a Vaadin Application in HTML Page</title>
  191. <!-- Set up the favicon from the Vaadin theme -->
  192. <link rel="shortcut icon" type="image/vnd.microsoft.icon"
  193. href="/VAADIN/themes/reindeer/favicon.ico" />
  194. <link rel="icon" type="image/vnd.microsoft.icon"
  195. href="/VAADIN/themes/reindeer/favicon.ico" />
  196. </head>
  197. <body>
  198. <!-- Loads the Vaadin widget set, etc. -->
  199. <script type="text/javascript"
  200. src="VAADIN/vaadinBootstrap.js"></script>
  201. <h1>Embedding a Vaadin UI</h1>
  202. <p>This is a static web page that contains an embedded Vaadin
  203. application. It's here:</p>
  204. <!-- So here comes the div element in which the Vaadin -->
  205. <!-- application is embedded. -->
  206. <div style="width: 300px; border: 2px solid green;"
  207. id="helloworld" class="v-app">
  208. <!-- Optional placeholder for the loading indicator -->
  209. <div class=" v-app-loading"></div>
  210. <!-- Alternative fallback text -->
  211. <noscript>You have to enable javascript in your browser to
  212. use an application built with Vaadin.</noscript>
  213. </div>
  214. <script type="text/javascript">//<![CDATA[
  215. if (!window.vaadin)
  216. alert("Failed to load the bootstrap JavaScript: "+
  217. "VAADIN/vaadinBootstrap.js");
  218. /* The UI Configuration */
  219. vaadin.initApplication("helloworld", {
  220. "browserDetailsUrl": "helloworld/",
  221. "serviceUrl": "helloworld/",
  222. "widgetset": "com.example.MyWidgetSet",
  223. "theme": "mytheme",
  224. "versionInfo": {"vaadinVersion": "8.1.6"},
  225. "vaadinDir": "VAADIN/",
  226. "heartbeatInterval": 300,
  227. "debug": true,
  228. "standalone": false,
  229. "authErrMsg": {
  230. "message": "Take note of any unsaved data, "+
  231. "and <u>click here<\/u> to continue.",
  232. "caption": "Authentication problem"
  233. },
  234. "comErrMsg": {
  235. "message": "Take note of any unsaved data, "+
  236. "and <u>click here<\/u> to continue.",
  237. "caption": "Communication problem"
  238. },
  239. "sessExpMsg": {
  240. "message": "Take note of any unsaved data, "+
  241. "and <u>click here<\/u> to continue.",
  242. "caption": "Session Expired"
  243. }
  244. });//]] >
  245. </script>
  246. <p>Please view the page source to see how embedding works.</p>
  247. </body>
  248. </html>
  249. ----
  250. endif::web[]
  251. [[advanced.embedding.iframe]]
  252. == Embedding Inside an [literal]#++iframe++# Element
  253. Embedding a Vaadin UI inside an [literal]#++<iframe>++# element is even easier
  254. than the method described above, as it does not require definition of any Vaadin
  255. specific definitions.
  256. You can embed an UI with an element such as the following:
  257. [subs="normal"]
  258. ----
  259. &lt;iframe src="[replaceable]#/myapp/myui#"&gt;&lt;/iframe&gt;
  260. ----
  261. The [literal]#++<iframe>++# elements have several downsides for embedding. One
  262. is that their size of is not flexible depending on the content of the frame, but
  263. the content must be flexible to accommodate in the frame. You can set the size
  264. of an [literal]#++<iframe>++# element with [literal]#++height++# and
  265. [literal]#++width++# attributes. Other issues arise from themeing and
  266. communication with the frame content and the rest of the page.
  267. Below is a complete example of using the [literal]#++<iframe>++# to embed two
  268. applications in a web page.
  269. [source, html]
  270. ----
  271. <!DOCTYPE html>
  272. <html>
  273. <head>
  274. <title>Embedding in IFrame</title>
  275. </head>
  276. <body style="background: #d0ffd0;">
  277. <h1>This is a HTML page</h1>
  278. <p>Below are two Vaadin applications embedded inside
  279. a table:</p>
  280. <table align="center" border="3">
  281. <tr>
  282. <th>The Calculator</th>
  283. <th>The Color Picker</th>
  284. </tr>
  285. <tr valign="top">
  286. <td>
  287. <iframe src="/vaadin-examples/Calc" height="200"
  288. width="150" frameborder="0"></iframe>
  289. </td>
  290. <td>
  291. <iframe src="/vaadin-examples/colorpicker"
  292. height="330" width="400"
  293. frameborder="0"></iframe>
  294. </td>
  295. </tr>
  296. </table>
  297. </body>
  298. </html>
  299. ----
  300. The page will look as shown in <<figure.embedding.iframe>> below.
  301. [[figure.embedding.iframe]]
  302. .Vaadin Applications Embedded Inside IFrames
  303. image::img/embedding3.png[]
  304. You can embed almost anything in an iframe, which essentially acts as a browser
  305. window. However, this creates various problems. The iframe must have a fixed
  306. size, inheritance of CSS from the embedding page is not possible, and neither is
  307. interaction with JavaScript, which makes mashups impossible, and so on. Even
  308. bookmarking with URI fragments will not work.
  309. Note also that websites can forbid iframe embedding by specifying an
  310. [literal]#++X-Frame-Options: SAMEORIGIN++# header in the HTTP response.
  311. ifdef::web[]
  312. [[advanced.embedding.xs]]
  313. == Cross-Site Embedding with the Vaadin XS Add-on
  314. __The XS add-on is currently not available for Vaadin 7.__
  315. In the previous sections, we described the two basic methods for embedding
  316. Vaadin applications: in a [literal]#++<div>++# element and in an
  317. [literal]#++<iframe>++#. One problem with div embedding is that it does not work
  318. between different Internet domains, which is a problem if you want to have your
  319. website running in one server and your Vaadin application in another. The
  320. security model in browsers effectively prevents such cross-site embedding of
  321. Ajax applications by enforcing the __same origin policy__ for XmlHttpRequest
  322. calls, even if the server is running in the same domain but different port.
  323. While iframe is more permissive, allowing embedding almost anything in anywhere,
  324. it has many disadvantanges, as described earlier.
  325. The Vaadin XS (Cross-Site) add-on works around the limitation in div embedding
  326. by using JSONP-style communication instead of the standard XmlHttpRequests.
  327. Embedding is done simply with:
  328. [source, html]
  329. ----
  330. <script src="http://demo.vaadin.com/xsembed/getEmbedJs"
  331. type="text/javascript"></script>
  332. ----
  333. This includes an automatically generated embedding script in the page, thereby
  334. making embedding effortless.
  335. This assumes that the main layout of the application has undefined height. If
  336. the height is 100%, you have to wrap it inside an element with a defined height.
  337. For example:
  338. [source, html]
  339. ----
  340. <div style="height: 500px;">
  341. <script src="http://demo.vaadin.com/xsembed/getEmbedJs"
  342. type="text/javascript"></script>
  343. </div>
  344. ----
  345. It is possible to restrict where the application can be embedded by using a
  346. whitelist. The add-on also encrypts the client-server communication, which is
  347. more important for embedded applications than usual.
  348. You can get the Vaadin XS add-on from Vaadin Directory. It is provided as a Zip
  349. package. Download and extract the installation package to a local folder.
  350. Instructions for installation and further information is given in the
  351. [filename]#README.html# file in the package.
  352. Some restrictions apply. You can have only one embedded application in one page.
  353. Also, some third-party libraries may interfere with the communication. Other
  354. notes are given in the README.
  355. endif::web[]