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:

Java

  1. import org.jsoup.nodes.Comment;
  2. import org.jsoup.nodes.Element;
  3. import org.jsoup.nodes.Node;
  4. import org.jsoup.parser.Tag;
  5. // ...
  6. new BootstrapListener() {
  7. @Override
  8. public void modifyBootstrapPage(BootstrapPageResponse response) {
  9. response.getDocument().body().appendChild(new Comment("Powered by Vaadin!", ""));
  10. }
  11. @Override
  12. public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
  13. // Wrap the fragment in a custom div element
  14. Element myDiv = new Element(Tag.valueOf("div"), "");
  15. List<Node> nodes = response.getFragmentNodes();
  16. for(Node node : nodes) {
  17. myDiv.appendChild(node);
  18. }
  19. nodes.clear();
  20. nodes.add(myDiv);
  21. }
  22. }

The HTML library we use is 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:

Java

  1. public void modifyBootstrapPage(BootstrapPageResponse response) {
  2. response.setHeader("X-Powered-By", "Vaadin 7");
  3. }

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.

Java

  1. class MyVaadinServlet extends VaadinServlet {
  2. @Override
  3. protected void servletInitialized() throws ServletException {
  4. super.servletInitialized();
  5. getService().addSessionInitListener(new SessionInitListener() {
  6. @Override
  7. public void sessionInit(SessionInitEvent event) {
  8. event.getSession().addBootstrapListener(listener);
  9. }
  10. });
  11. }
  12. }
  13. // Or...
  14. class MyVaadinPortlet extends VaadinPortlet {
  15. @Override
  16. protected void portletInitialized() throws PortletException {
  17. super.portletInitialized();
  18. getService().addSessionInitListener(new SessionInitListener() {
  19. @Override
  20. public void sessionInit(SessionInitEvent event) {
  21. event.getSession().addBootstrapListener(listener);
  22. }
  23. });
  24. }
  25. }