Importing HTML and JavaScript
There are two ways to import HTML and JavaScript with a component.
By using the annotations @JavaScript and @HtmlImport
Java
@Tag("div")
@JavaScript("/js/script.js")
@HtmlImport("/html/htmlimport.html")
static class HtmlComponent extends Component implements HasText {
// implementation omitted
}
Both annotations are repeatable and you can add multiple annotations for both to the component.
Another way of adding imports are the addHtmlImport(String url)
and addJavaScript(String url)
methods from the Page
class. The functionality is the same as for the annotations.
Java
private void addDependencies() {
UI.getCurrent().getPage().addHtmlImport("/html/htmlimport.html");
UI.getCurrent().getPage().addJavaScript("/js/script.js");
}
Note | For every class that imports dependencies, import order is guaranteed for dependencies of the same type only. No guarantees are given regarding numerous classes’ dependencies’ import order, same as no guarantees are given on import order of dependencies of not the same type. Example:
In this case, If you want to enforce an order between dependencies of a different type, you can add html import with imports in it. For example, if
|
You can place your static resources in any folder inside your WAR file except for /VAADIN
which is reserved for framework internal use. VaadinServlet
handles static resource requests if you have mapped it to /*
. Otherwise, the servlet container will take care of static resource requests.
By using relative URLs you are not dependent on whether the application is deployed in the root context (e.g. [http://mysite.com/](http://mysite.com/)
) or in a sub context (e.g. [http://mysite.com/myapp/](http://mysite.com/myapp/)
).
Relative URLs are resolved using the page base URI, which is always set to match the servlet URL.
Tip | If you are using a servlet path for the servlet, e.g. You can use the special protocol When you configure an element, e.g setting the
|
Tip | There is a possibility to define which dependencies are loaded first, refer to Ways of importing the dependencies for details. |