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.

CustomizingTheStartupPageInAnApplication.asciidoc 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ---
  2. title: Customizing The Startup Page In An Application
  3. order: 25
  4. layout: page
  5. ---
  6. [[customizing-the-startup-page-in-an-application]]
  7. = Customizing the startup page in an application
  8. In Vaadin 6, the startup page - used to bootstrap a new Vaadin UI
  9. instance in the browser - was generated as a monolithic chunk of HTML
  10. and was not easily customizable. In Vaadin 7, we added a new facility
  11. for registering special _bootstrap listeners_ that are invoked before
  12. the bootstrap response is sent. In addition, instead of bare HTML in a
  13. string, the response is now built as a DOM tree that is easy to
  14. manipulate programmatically.
  15. Here's an example of a simple bootstrap listener:
  16. [source,java]
  17. ....
  18. import org.jsoup.nodes.Comment;
  19. import org.jsoup.nodes.Element;
  20. import org.jsoup.nodes.Node;
  21. import org.jsoup.parser.Tag;
  22. // ...
  23. new BootstrapListener() {
  24. @Override
  25. public void modifyBootstrapPage(BootstrapPageResponse response) {
  26. response.getDocument().body().appendChild(new Comment("Powered by Vaadin!", ""));
  27. }
  28. @Override
  29. public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
  30. // Wrap the fragment in a custom div element
  31. Element myDiv = new Element(Tag.valueOf("div"), "");
  32. List<Node> nodes = response.getFragmentNodes();
  33. for(Node node : nodes) {
  34. myDiv.appendChild(node);
  35. }
  36. nodes.clear();
  37. nodes.add(myDiv);
  38. }
  39. }
  40. ....
  41. The HTML library we use is http://jsoup.org/[jsoup]. It provides a very
  42. convenient API for traversing, manipulating and extracting data from a
  43. DOM, and is HTML5 compliant.
  44. The `BootstrapListener` interface contains two methods, one of which is
  45. usually left empty. This is because a Vaadin application can be either
  46. stand-alone, in which case it "owns" the whole page its UI resides in,
  47. or embedded, such as a portlet, in which case it does not control the
  48. content of the page it is embedded in.
  49. The `modifyBootstrapFragment` method is called in both cases. It
  50. receives a `BootstrapFragmentResponse` that represents the HTML fragment
  51. that is inserted in the host page, whether the page is controlled by
  52. Vaadin or not. Hence, you only need to implement this method if you do
  53. not care about the host page, whether your application is embedded or
  54. standalone.
  55. The `modifyBootstrapPage` method is called with a
  56. `BootstrapPageResponse` argument that represents the whole bootstrap
  57. page, including the fragment mentioned above. Thus, it is only invoked
  58. when the application is standalone and actually responsible for
  59. generating the page. This method allows you to, for instance, add things
  60. to the `head` element. The `BootstrapPageResponse` class also allows
  61. setting arbitrary HTTP response headers:
  62. [source,java]
  63. ....
  64. public void modifyBootstrapPage(BootstrapPageResponse response) {
  65. response.setHeader("X-Powered-By", "Vaadin 7");
  66. }
  67. ....
  68. But how and where should the bootstrap listeners be registered? It
  69. should be only once per session, and right in the beginning, so that
  70. they are already added when the first response is sent.
  71. To do that you should write a custom servlet that extends
  72. `VaadinServlet`, or a custom portlet extending `VaadinPortlet`, and a
  73. session init listener that adds the bootstrap listener to the new
  74. session.
  75. [source,java]
  76. ....
  77. class MyVaadinServlet extends VaadinServlet {
  78. @Override
  79. protected void servletInitialized() throws ServletException {
  80. super.servletInitialized();
  81. getService().addSessionInitListener(new SessionInitListener() {
  82. @Override
  83. public void sessionInit(SessionInitEvent event) {
  84. event.getSession().addBootstrapListener(listener);
  85. }
  86. });
  87. }
  88. }
  89. // Or...
  90. class MyVaadinPortlet extends VaadinPortlet {
  91. @Override
  92. protected void portletInitialized() throws PortletException {
  93. super.portletInitialized();
  94. getService().addSessionInitListener(new SessionInitListener() {
  95. @Override
  96. public void sessionInit(SessionInitEvent event) {
  97. event.getSession().addBootstrapListener(listener);
  98. }
  99. });
  100. }
  101. }
  102. ....