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.

intro-walkthrough.asciidoc 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ---
  2. title: Example Application Walkthrough
  3. order: 2
  4. layout: page
  5. ---
  6. [[intro.walkthrough]]
  7. = Example Application Walkthrough
  8. Let us follow the long tradition of first saying "Hello World!" when learning a
  9. new programming framework.
  10. First, using the primary server-side API.
  11. [source, java]
  12. ----
  13. import com.vaadin.server.VaadinRequest;
  14. import com.vaadin.ui.Label;
  15. import com.vaadin.ui.UI;
  16. @Title("My UI")
  17. @Theme("valo")
  18. public class HelloWorld extends UI {
  19. @Override
  20. protected void init(VaadinRequest request) {
  21. // Create the content root layout for the UI
  22. VerticalLayout content = new VerticalLayout();
  23. setContent(content);
  24. // Display the greeting
  25. content.addComponent(new Label("Hello World!"));
  26. // Have a clickable button
  27. content.addComponent(new Button("Push Me!",
  28. new ClickListener() {
  29. @Override
  30. public void buttonClick(ClickEvent e) {
  31. Notification.show("Pushed!");
  32. }
  33. }));
  34. }
  35. }
  36. ----
  37. A Vaadin application has one or more __UI__s that extend the
  38. [classname]#com.vaadin.ui.UI# class. A UI is a part of the web page in which the
  39. Vaadin application runs. An application can have multiple UIs in the same page,
  40. especially in portals, or in different windows or tabs. A UI is associated with
  41. a user session, and a session is created for each user who uses the application.
  42. In the context of our Hello World UI, it is sufficient to know that the
  43. underlying session is created when the user first accesses the application by
  44. opening the page, and the [methodname]#init()# method is invoked at that time.
  45. The page title, which is shown in the caption of the browser window or tab, is
  46. defined with an annotation. The example uses a layout component as the root
  47. content of the UI, as that is the case with most Vaadin applications, which
  48. normally have more than one component. It then creates a new [classname]#Label#
  49. user interface component, which displays simple text, and sets the text to
  50. "Hello World!". The label is added to the layout.
  51. The example also shows how to create a button and handle button click events.
  52. Event handling is described in
  53. <<dummy/../../../framework/architecture/architecture-events#architecture.events,"Events
  54. and Listeners">> and on the practical side in
  55. <<dummy/../../../framework/application/application-events#application.events,"Handling
  56. Events with Listeners">>. In addition to listeners, in Java 8 you can handle
  57. events with lambda expressions, which simplifies the handler code significantly.
  58. [source, java]
  59. ----
  60. content.addComponent(new Button("Push Me!",
  61. event -> Notification.show("Pushed!")));
  62. ----
  63. The result of the Hello World application, when opened in a browser, is shown in
  64. <<figure.intro.walkthrough>>.
  65. [[figure.intro.walkthrough]]
  66. .Hello World Application
  67. image::img/HelloWorld.png[scaledwidth=70%]
  68. To run a program, you need to package it as a web application WAR package and
  69. deploy it to a server, as explained in
  70. <<dummy/../../../framework/application/application-environment#application.environment,"Deploying
  71. an Application">>. During development, you typically deploy to an application
  72. server integrated with the IDE.
  73. Developing a pure client-side application, you could write a Hello World just as
  74. easily, and also in Java:
  75. [source, java]
  76. ----
  77. public class HelloWorld implements EntryPoint {
  78. @Override
  79. public void onModuleLoad() {
  80. RootPanel.get().add(new Label("Hello, world!"));
  81. }
  82. }
  83. ----
  84. We do not set the title here, because it is usually defined in the HTML page in
  85. which the code is executed. The application would be compiled into JavaScript
  86. with the Vaadin Client Compiler (or GWT Compiler). It is more typical, however,
  87. to write client-side widgets, which you can then use from a server-side Vaadin
  88. application. For more information regarding client-side development, see
  89. <<dummy/../../../framework/clientside/clientside-overview.asciidoc#clientside.overview,"Client-Side
  90. Vaadin Development">>.