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 18KB

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