++# method allows such flexibility. The following sections
look closer into these two embedding methods.
[[advanced.embedding.div]]
== Embedding Inside a [literal]#++div++# Element
You can embed one or more Vaadin UIs inside a web page with a method that is
equivalent to loading the initial page content from the Vaadin servlet in a
non-embedded UI. Normally, the [classname]#VaadinServlet# generates an initial
page that contains the correct parameters for the specific UI. You can easily
configure it to load multiple Vaadin UIs in the same page. They can have
different widget sets and different themes.
Embedding an UI requires the following basic tasks:
* Set up the page header
* Include a GWT history frame in the page
* Call the [filename]#vaadinBootstrap.js# file
* Define the [literal]#++
++# element for the UI
* Configure and initialize the UI
Notice that you can view the loader page for the UI easily by opening the UI in
a web browser and viewing the HTML source code of the page. You could just copy
and paste the embedding code from the page, but some modifications and
additional settings are required, mainly related to the URLs that have to be
made relative to the page instead of the servlet URL.
ifdef::web[]
[[advanced.embedding.div.head]]
=== The Head Matter
The HTML page in which the Vaadin UI is embedded should be a valid HTML 5
document. The content of the head element is largely up to you. The character
encoding must be UTF-8. Some meta declarations are necessary for compatibility.
You can also set the page favicon in the head element.
[subs="normal"]
----
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=9;chrome=1" />
<title>[replaceable]#This is my Embedding Page#</title>
<!-- Set up the favicon from the Vaadin theme -->
<link rel="shortcut icon" type="image/vnd.microsoft.icon"
href="/VAADIN/themes/[replaceable]#reindeer#/favicon.ico" />
<link rel="icon" type="image/vnd.microsoft.icon"
href="/VAADIN/themes/[replaceable]#reindeer#/favicon.ico" />
</head>
----
endif::web[]
ifdef::web[]
[[advanced.embedding.div.body]]
=== The Body Matter
The page content must include some Vaadin-related definitions before you can
embed Vaadin UIs in it.
The [filename]#vaadinBootstrap.js# script makes definitions for starting up the
UI. It must be called before initializing the UI. The source path must be
relative to the path of the embedding page.
[subs="normal"]
----
<body>
<script type="text/javascript"
src="[replaceable]#./#VAADIN/vaadinBootstrap.js"></script>
----
The bootstrap script is served by the Vaadin servlet from inside the
[filename]#vaadin-server# JAR.
Vaadin, or more precisely GWT, requires an invisible history frame, which is
used for tracking the page or fragment history in the browser.
[source, html]
----
----
endif::web[]
ifdef::web[]
[[advanced.embedding.div.div]]
=== UI Placeholder Element
A Vaadin UI is embedded in a placeholder [literal]#++
++# element. It should
have the following features:
* The [literal]#++
++# 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.
* It must have at least the [literal]#++v-app++# style class.
* it should have a nested [literal]#++
++# 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.
* It should also contain a [literal]#++
++# 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.
The placeholder element can include style settings, typically a width and
height. If the sizes are not defined, the UI will have an undefined size in the
particular dimension, which must be in accordance with the sizing of the UI
components.
For example:
[subs="normal"]
----
<div style="[replaceable]#width: 300px; border: 2px solid green;#"
id="helloworldui" class="v-app">
<div class="v-app-loading"></div>
<noscript>[replaceable]#You have to enable javascript in your browser to#
[replaceable]#use an application built with Vaadin.#</noscript>
</div>
----
endif::web[]
ifdef::web[]
[[advanced.embedding.div.init]]
=== Initializing the UI
The UI is loaded by calling the [literal]#++initApplication()++# method for the
[literal]#++vaadin++# object defined in the bootstrap script. Before calling it,
you should check that the bootstrap script was loaded properly.
[subs="normal"]
----
<script type="text/javascript">//<![CDATA[
if (!window.vaadin)
alert("[replaceable]#Failed to load the bootstrap JavaScript:#"+
"[replaceable]#VAADIN/vaadinBootstrap.js#");
----
The [literal]#++initApplication()++# takes two parameters. The first parameter
is the UI identifier, exactly as given as the [literal]#++id++# attribute of the
placeholder element. The second parameter is an associative map that contains
parameters for the UI.
The map must contain the following items:
[parameter]#browserDetailsUrl#:: This should be the URL path (relative to the embedding page) to the Vaadin
servlet of the UI. It is used by the bootstrap to communicate browser details. A
trailing slash may be needed in some cases.
+
Notice that this parameter is not included in the loader page generated by the
servlet, because in that case, it can default to the current URL.
[parameter]#serviceUrl#:: This is used for server requests after initial loading and should be same as for
[parameter]#browserDetailsUrl#. The two parameters are redundant and either may
be removed in
future.+
//Bug
#10122
[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#.
[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.
[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.
[parameter]#vaadinDir#:: Relative path to the [filename]#VAADIN# directory. It is relative to the URL of the embedding page.
[parameter]#heartbeatInterval#:: The [parameter]#hearbeatInterval# parameter defines the frequency of the keep-alive hearbeat for the UI in seconds, as described in <>.
[parameter]#debug#:: The parameter defines whether the debug window, as described in <>, is enabled.
[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.
[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.
For example:
[subs="normal"]
----
vaadin.initApplication("[replaceable]#helloworldui#", {
"browserDetailsUrl": "[replaceable]#helloworld#/",
"serviceUrl": "[replaceable]#helloworld#/",
"widgetset": "[replaceable]#com.example.MyWidgetSet#",
"theme": "[replaceable]#mytheme#",
"versionInfo": {"vaadinVersion": "[replaceable]#7.0.0#"},
"vaadinDir": "[replaceable]#VAADIN/#",
"heartbeatInterval": [replaceable]#300#,
"debug": [replaceable]#true#,
"standalone": false,
"authErrMsg": {
"message": "[replaceable]#Take note of any unsaved data, "+ "and <u>click here<\/u> to continue.#",
"caption": "Authentication problem"
},
"comErrMsg": {
"message": "[replaceable]#Take note of any unsaved data, "+ "and <u>click here<\/u> to continue.#",
"caption": "Communication problem"
},
"sessExpMsg": {
"message": "[replaceable]#Take note of any unsaved data, "+ "and <u>click here<\/u> to continue.#",
"caption": "Session Expired"
}
});//]]>
</script>
----
Notice that many of the parameters are normally deployment parameters, specified
in the deployment descriptor, as described in
<>.
endif::web[]
ifdef::web[]
[[advanced.embedding.div.summary]]
=== Summary of Div Embedding
Below is a complete example of embedding an UI in a [literal]#++++#
element.
[source, html]
----
Embedding a Vaadin Application in HTML Page
Embedding a Vaadin UI
This is a static web page that contains an embedded Vaadin
application. It's here:
You have to enable javascript in your browser to
use an application built with Vaadin.
Please view the page source to see how embedding works.
----
endif::web[]
[[advanced.embedding.iframe]]
== Embedding Inside an [literal]#++iframe++# Element
Embedding a Vaadin UI inside an [literal]#++