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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. public class HelloWorld extends UI {
  18. @Override
  19. protected void init(VaadinRequest request) {
  20. // Create the content root layout for the UI
  21. VerticalLayout content = new VerticalLayout();
  22. setContent(content);
  23. // Display the greeting
  24. content.addComponent(new Label("Hello World!"));
  25. // Have a clickable button
  26. content.addComponent(new Button("Push Me!",
  27. click -> Notification.show("Pushed!")));
  28. }
  29. }
  30. ----
  31. A Vaadin application has one or more __UI__s that extend the
  32. [classname]#com.vaadin.ui.UI# class. A UI is a part of the web page in which the
  33. Vaadin application runs. An application can have multiple UIs in the same page,
  34. especially in portals, or in different windows or tabs. A UI is associated with
  35. a user session, and a session is created for each user who uses the application.
  36. In the context of our Hello World UI, it is sufficient to know that the
  37. underlying session is created when the user first accesses the application by
  38. opening the page, and the [methodname]#init()# method is invoked at that time.
  39. The page title, which is shown in the caption of the browser window or tab, is
  40. defined with an annotation. The example uses a layout component as the root
  41. content of the UI, as that is the case with most Vaadin applications, which
  42. normally have more than one component. It then creates a new [classname]#Label#
  43. user interface component, which displays simple text, and sets the text to
  44. "Hello World!". The label is added to the layout.
  45. The example also shows how to create a button and handle button click events.
  46. Event handling is described in
  47. <<../architecture/architecture-events#architecture.events,"Events and Listeners">> and on the practical side in <<../application/application-events#application.events,"Handling Events with Listeners">>.
  48. In Java 8, you can implement listeners with lambda expressions, which simplifies the handler code significantly.
  49. The result of the Hello World application, when opened in a browser, is shown in
  50. <<figure.intro.walkthrough>>.
  51. [[figure.intro.walkthrough]]
  52. .Hello World Application
  53. image::img/HelloWorld.png[scaledwidth=70%]
  54. To run a program, you need to package it as a web application WAR package and
  55. deploy it to a server, as explained in
  56. <<../application/application-environment#application.environment,"Deploying
  57. an Application">>. During development, you typically deploy to an application
  58. server integrated with the IDE.