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

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