原文: https://howtodoinjava.com/vaadin/hello-world-web-application-maven/

在此示例中,我们将学习使用 maven 创建 vaadin helloworld 应用,然后在内置Jetty服务器中运行部署该应用。

使用 Maven 原型创建 vaadin 项目

在工作区中使用下面的 maven 命令创建最简单的 vaadin Web 应用。

  1. $ mvn archetype:generate
  2. -DarchetypeGroupId=com.vaadin
  3. -DarchetypeArtifactId=vaadin-archetype-application
  4. -DarchetypeVersion=LATEST
  5. -DgroupId=com.howtodoinjava.vaadin.demo
  6. -DartifactId=VaadinExample
  7. -Dversion=1.0
  8. -Dpackaging=war

请根据需要更新-DgroupId-DartifactId

生成的项目结构

现在,将生成的项目作为现有的 maven 项目导入到您的 IDE(在我的情况下为 eclipse)中。

使用 Maven 的 vaadin HelloWorld Web 应用 - 图1

Vaadin HelloWorld 应用项目结构

生成的项目包含任何 Maven Web 应用的标准文件夹结构。

生成的文件

连同文件夹结构一起,我们将获得这些生成的文件以及 vaadin helloworld 项目

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.howtodoinjava.vaadin.demo</groupId>
  6. <artifactId>VaadinExample</artifactId>
  7. <packaging>war</packaging>
  8. <version>1.0</version>
  9. <name>VaadinExample</name>
  10. <prerequisites>
  11. <maven>3</maven>
  12. </prerequisites>
  13. <properties>
  14. <vaadin.version>7.7.0</vaadin.version>
  15. <vaadin.plugin.version>7.7.0</vaadin.plugin.version>
  16. <jetty.plugin.version>9.3.9.v20160517</jetty.plugin.version>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <maven.compiler.source>1.8</maven.compiler.source>
  19. <maven.compiler.target>1.8</maven.compiler.target>
  20. <!-- If there are no local customisations, this can also be "fetch" or "cdn" -->
  21. <vaadin.widgetset.mode>local</vaadin.widgetset.mode>
  22. </properties>
  23. <repositories>
  24. <repository>
  25. <id>vaadin-addons</id>
  26. <url>http://maven.vaadin.com/vaadin-addons</url>
  27. </repository>
  28. <repository>
  29. <id>vaadin-snapshots</id>
  30. <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
  31. <releases>
  32. <enabled>false</enabled>
  33. </releases>
  34. <snapshots>
  35. <enabled>true</enabled>
  36. </snapshots>
  37. </repository>
  38. </repositories>
  39. <dependencyManagement>
  40. <dependencies>
  41. <dependency>
  42. <groupId>com.vaadin</groupId>
  43. <artifactId>vaadin-bom</artifactId>
  44. <version>${vaadin.version}</version>
  45. <type>pom</type>
  46. <scope>import</scope>
  47. </dependency>
  48. </dependencies>
  49. </dependencyManagement>
  50. <dependencies>
  51. <dependency>
  52. <groupId>javax.servlet</groupId>
  53. <artifactId>javax.servlet-api</artifactId>
  54. <version>3.0.1</version>
  55. <scope>provided</scope>
  56. </dependency>
  57. <dependency>
  58. <groupId>com.vaadin</groupId>
  59. <artifactId>vaadin-server</artifactId>
  60. </dependency>
  61. <dependency>
  62. <groupId>com.vaadin</groupId>
  63. <artifactId>vaadin-push</artifactId>
  64. </dependency>
  65. <dependency>
  66. <groupId>com.vaadin</groupId>
  67. <artifactId>vaadin-client-compiled</artifactId>
  68. </dependency>
  69. <dependency>
  70. <groupId>com.vaadin</groupId>
  71. <artifactId>vaadin-themes</artifactId>
  72. </dependency>
  73. </dependencies>
  74. <build>
  75. <plugins>
  76. <plugin>
  77. <groupId>org.apache.maven.plugins</groupId>
  78. <artifactId>maven-war-plugin</artifactId>
  79. <version>2.6</version>
  80. <configuration>
  81. <failOnMissingWebXml>false</failOnMissingWebXml>
  82. <!-- Exclude an unnecessary file generated by the GWT compiler. -->
  83. <packagingExcludes>WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
  84. </configuration>
  85. </plugin>
  86. <plugin>
  87. <groupId>com.vaadin</groupId>
  88. <artifactId>vaadin-maven-plugin</artifactId>
  89. <version>${vaadin.plugin.version}</version>
  90. <executions>
  91. <execution>
  92. <goals>
  93. <goal>update-theme</goal>
  94. <goal>update-widgetset</goal>
  95. <goal>compile</goal>
  96. <!-- Comment out compile-theme goal to use on-the-fly theme compilation -->
  97. <goal>compile-theme</goal>
  98. </goals>
  99. </execution>
  100. </executions>
  101. </plugin>
  102. <plugin>
  103. <groupId>org.apache.maven.plugins</groupId>
  104. <artifactId>maven-clean-plugin</artifactId>
  105. <version>3.0.0</version>
  106. <!-- Clean up also any pre-compiled themes -->
  107. <configuration>
  108. <filesets>
  109. <fileset>
  110. <directory>src/main/webapp/VAADIN/themes</directory>
  111. <includes>
  112. <include>**/styles.css</include>
  113. <include>**/styles.scss.cache</include>
  114. </includes>
  115. </fileset>
  116. </filesets>
  117. </configuration>
  118. </plugin>
  119. <!-- The Jetty plugin allows us to easily test the development build by
  120. running jetty:run on the command line. -->
  121. <plugin>
  122. <groupId>org.eclipse.jetty</groupId>
  123. <artifactId>jetty-maven-plugin</artifactId>
  124. <version>${jetty.plugin.version}</version>
  125. <configuration>
  126. <scanIntervalSeconds>2</scanIntervalSeconds>
  127. </configuration>
  128. </plugin>
  129. </plugins>
  130. </build>
  131. <profiles>
  132. <profile>
  133. <!-- Vaadin pre-release repositories -->
  134. <id>vaadin-prerelease</id>
  135. <activation>
  136. <activeByDefault>false</activeByDefault>
  137. </activation>
  138. <repositories>
  139. <repository>
  140. <id>vaadin-prereleases</id>
  141. <url>http://maven.vaadin.com/vaadin-prereleases</url>
  142. </repository>
  143. </repositories>
  144. <pluginRepositories>
  145. <pluginRepository>
  146. <id>vaadin-prereleases</id>
  147. <url>http://maven.vaadin.com/vaadin-prereleases</url>
  148. </pluginRepository>
  149. </pluginRepositories>
  150. </profile>
  151. </profiles>
  152. </project>

MyUI.java

  1. package com.howtodoinjava.vaadin.demo;
  2. import javax.servlet.annotation.WebServlet;
  3. import com.vaadin.annotations.Theme;
  4. import com.vaadin.annotations.VaadinServletConfiguration;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.server.VaadinServlet;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.Label;
  9. import com.vaadin.ui.TextField;
  10. import com.vaadin.ui.UI;
  11. import com.vaadin.ui.VerticalLayout;
  12. /**
  13. * This UI is the application entry point. A UI may either represent a browser window
  14. * (or tab) or some part of a html page where a Vaadin application is embedded.
  15. * <p>
  16. * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
  17. * overridden to add component to the user interface and initialize non-component functionality.
  18. */
  19. @Theme("mytheme")
  20. public class MyUI extends UI {
  21. @Override
  22. protected void init(VaadinRequest vaadinRequest) {
  23. final VerticalLayout layout = new VerticalLayout();
  24. final TextField name = new TextField();
  25. name.setCaption("Type your name here:");
  26. Button button = new Button("Click Me");
  27. button.addClickListener( e -> {
  28. layout.addComponent(new Label("Thanks " + name.getValue()
  29. + ", it works!"));
  30. });
  31. layout.addComponents(name, button);
  32. layout.setMargin(true);
  33. layout.setSpacing(true);
  34. setContent(layout);
  35. }
  36. @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
  37. @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
  38. public static class MyUIServlet extends VaadinServlet {
  39. }
  40. }

如果要使用web.xml文件,因为您的服务器不支持 servlet 3.0 规范,则可以在web.xml文件中使用此配置。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <servlet>
  7. <servlet-name>myservlet</servlet-name>
  8. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  9. <init-param>
  10. <param-name>UI</param-name>
  11. <param-value>com.howtodoinjava.vaadin.demo.MyUI</param-value>
  12. </init-param>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>myservlet</servlet-name>
  16. <url-pattern>/*</url-pattern>
  17. </servlet-mapping>
  18. </web-app>

如果您正在使用web.xml文件,请不要忘记删除@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)

构建生成的项目

现在该构建项目,以便它可以下载所有依赖项并将其包含在项目的运行时中。

  1. $ mvn clean install

上面的 maven 命令将下载依赖项(需要一些时间),并构建 war 文件VaadinExample-1.0.war

运行并测试 vaadin HelloWorld 应用

现在该运行该应用了。 在命令提示符下,运行以下命令以启动随附的 Jetty 服务器。

  1. $mvn jetty:run

这将启动内置的 Jetty 服务器,您可以通过http://localhost:8080/访问该应用。

使用 Maven 的 vaadin HelloWorld Web 应用 - 图2

Vaadin 你好世界屏幕

现在在文本框中填写您的姓名或任何字符串,然后单击按钮。 它将在按钮下方打印消息。

使用 Maven 的 vaadin HelloWorld Web 应用 - 图3

Vaadin 你好世界屏幕 - 2

下载源码

学习愉快!

资源:

Vaadin 原型

Vaadin Maven 设置