Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IBGettingStartedWithVaadinSpringWithoutSpringBoot.asciidoc 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ---
  2. title: Getting Started With Vaadin Spring Without Spring Boot
  3. order: 43
  4. layout: page
  5. ---
  6. [[i-b-getting-started-with-vaadin-spring-without-spring-boot]]
  7. I b - Getting started with Vaadin Spring without Spring Boot
  8. ------------------------------------------------------------
  9. Note: this tutorial applies to *Vaadin Spring 1.0.0 and later*
  10. During this tutorial we will create a new Vaadin project, add Spring and
  11. Vaadin Spring as dependencies, create a simple Spring UI and deploy the
  12. application to a server. 
  13. *Note that this tutorial is for using Vaadin Spring without Spring Boot.
  14. Using Spring Boot is the recommended approach for getting started
  15. quickly when creating a new project. For more information about setting
  16. up a project both with and without Spring Boot -
  17. see https://vaadin.github.io/spring-tutorial/[the Vaadin Spring Boot tutorial].*
  18. [[creating-a-vaadin-project]]
  19. Creating a Vaadin project
  20. ~~~~~~~~~~~~~~~~~~~~~~~~~
  21. If you've created Vaadin projects before, there's nothing new here.
  22. File→New→Project... then select Maven Project. This will take you to the
  23. project creation wizard. You can also create the project using Vaadin
  24. Plug-in for Eclipse if you prefer. 
  25. image:img/project-creation.png[Project creation]
  26. then select the Vaadin archetype
  27. (`com.vaadin:vaadin-archetype-application:<newest Vaadin version>`).
  28. Set your group and artefact ids and your package to your liking. (I used
  29. `com.vaadin`, `spring.tutorial` and `com.vaadin.spring.tutorial` respectively)
  30. [[adding-vaadin-spring-as-a-dependency]]
  31. Adding Vaadin Spring as a dependency
  32. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  33. Open pom.xml and add
  34. [source,xml]
  35. ....
  36. <dependency>
  37. <groupId>com.vaadin</groupId>
  38. <artifactId>vaadin-spring</artifactId>
  39. <version>1.0.0</version>
  40. </dependency>
  41. ....
  42. in the dependencies. Also replace the dependency "vaadin-client" with
  43. "vaadin-client-compiled" and remove the scope line for it as we are
  44. using a pre-compiled widgetset. If your project does not use the Vaadin
  45. add-on Maven repository yet, add it to the POM:
  46. [source,xml]
  47. ....
  48. <repositories>
  49. <repository>
  50. <id>vaadin-addons</id>
  51. <url>http://maven.vaadin.com/vaadin-addons</url>
  52. </repository>
  53. </repositories>
  54. ....
  55. Then save and update project.
  56. [[creating-the-ui]]
  57. Creating the UI
  58. ~~~~~~~~~~~~~~~
  59. The project wizard created a UI for us that we'll use as a starting
  60. point for building our application. There are some unnecessary things
  61. we'll take out and some things we'll need to add. Start of by deleting
  62. the AppWidgetSet.gwt.xml, as we won't need a custom widgetset in these
  63. tutorials. We'll also have to make a few changes to the UI to make it
  64. work with Vaadin Spring.
  65. Here's the UI's original source:
  66. [source,java]
  67. ....
  68. @Theme("mytheme")
  69. @SuppressWarnings("serial")
  70. public class MyVaadinUI extends UI {
  71. @WebServlet(value = "/*", asyncSupported = true)
  72. @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "com.vaadin.spring.tutorial.AppWidgetSet")
  73. public static class Servlet extends VaadinServlet {
  74. }
  75. @Override
  76. protected void init(VaadinRequest request) {
  77. final VerticalLayout layout = new VerticalLayout();
  78. layout.setMargin(true);
  79. setContent(layout);
  80. Button button = new Button("Click Me");
  81. button.addClickListener(new Button.ClickListener() {
  82. public void buttonClick(ClickEvent event) {
  83. layout.addComponent(new Label("Thank you for clicking"));
  84. }
  85. });
  86. layout.addComponent(button);
  87. }
  88. }
  89. ....
  90. To allow Vaadin Spring to use the UI you'll need to add the following
  91. annotation to the UI:
  92. [source,java]
  93. ....
  94. @SpringUI
  95. ....
  96. The servlet configuration needs to be updated to initialize a Spring
  97. application context, and the servlet should inherit from
  98. `SpringVaadinServlet`. In this tutorial, a `ContextLoaderListener` is used
  99. to initialize Spring.
  100. Finally, as we do not need a custom theme in the application, the theme
  101. annotation is updated to use "valo" and the custom theme in the project
  102. can be deleted.
  103. The resulting UI should be something like this:
  104. [source,java]
  105. ....
  106. package com.vaadin.spring.tutorial;
  107. import javax.servlet.annotation.WebListener;
  108. import javax.servlet.annotation.WebServlet;
  109. import org.springframework.context.annotation.Configuration;
  110. import org.springframework.web.context.ContextLoaderListener;
  111. import com.vaadin.annotations.Theme;
  112. import com.vaadin.server.VaadinRequest;
  113. import com.vaadin.spring.annotation.EnableVaadin;
  114. import com.vaadin.spring.annotation.SpringUI;
  115. import com.vaadin.spring.server.SpringVaadinServlet;
  116. import com.vaadin.ui.Button;
  117. import com.vaadin.ui.Button.ClickEvent;
  118. import com.vaadin.ui.Label;
  119. import com.vaadin.ui.UI;
  120. import com.vaadin.ui.VerticalLayout;
  121. @Theme("valo")
  122. @SpringUI
  123. @SuppressWarnings("serial")
  124. public class MyVaadinUI extends UI {
  125. @WebServlet(value = "/*", asyncSupported = true)
  126. public static class Servlet extends SpringVaadinServlet {
  127. }
  128. @WebListener
  129. public static class MyContextLoaderListener extends ContextLoaderListener {
  130. }
  131. @Configuration
  132. @EnableVaadin
  133. public static class MyConfiguration {
  134. }
  135. @Override
  136. protected void init(VaadinRequest request) {
  137. final VerticalLayout layout = new VerticalLayout();
  138. layout.setMargin(true);
  139. setContent(layout);
  140. Button button = new Button("Click Me");
  141. button.addClickListener(new Button.ClickListener() {
  142. public void buttonClick(ClickEvent event) {
  143. layout.addComponent(new Label("Thank you for clicking"));
  144. }
  145. });
  146. layout.addComponent(button);
  147. }
  148. }
  149. ....
  150. With the `@SpringUI` annotation the Vaadin Spring plugin will know to
  151. inject the UI rather than directly instantiating it. With injected beans
  152. we can use all of the usual Spring features such as autowiring. More on
  153. that in later tutorials.
  154. In addition to these changes, when not using Spring Boot, create the
  155. following Spring context file at
  156. src/main/webapp/WEB-INF/applicationContext.xml :
  157. [source,xml]
  158. ....
  159. <?xml version="1.0" encoding="UTF-8"?>
  160. <beans xmlns="http://www.springframework.org/schema/beans"
  161. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  162. xsi:schemaLocation="http://www.springframework.org/schema/beans
  163. http://www.springframework.org/schema/beans/spring-beans.xsd
  164. http://www.springframework.org/schema/context
  165. http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  166. <bean class="com.vaadin.spring.tutorial.MyVaadinUI.MyConfiguration" />
  167. <context:component-scan base-package="com.vaadin.spring.tutorial" />
  168. </beans>
  169. ....
  170. A full description of alternative approaches to configuring Spring is
  171. outside the context of this tutorial and you should consult Spring
  172. documentation for them, but a brief introduction to them is given in
  173. https://vaadin.github.io/spring-tutorial/[this
  174. tutorial].
  175. [[deployment]]
  176. Deployment
  177. ~~~~~~~~~~
  178. Once the UI is done we'll deploy it to our server by Run→Run as→Run on
  179. Server. Select your server runtime (Tomcat in our case) and click
  180. Finish.
  181. Eclipse should automatically open an embedded browser directed at your
  182. development server.
  183. Congratulations! You've deployed your first Spring application.