GWT - 部署应用程序

本教程将向您解释如何创建应用程序 "war" 文件以及如何在 Apache Tomcat Websever 根目录中部署该文件。

如果您理解了这个简单示例,那么您也将能够按照相同的步骤部署复杂的 GWT 应用程序。

让我们使用 Eclipse IDE 和 GWT 插件,并按照以下步骤创建 GWT 应用程序 −

步骤 描述
1 com.tutorialspoint 包下创建一个名为 HelloWorld 的项目,如 GWT - 创建应用程序 中所述章节。
2 按照以下说明修改 HelloWorld.gwt.xmlHelloWorld.cssHelloWorld.htmlHelloWorld.java。保持其余文件不变。
3 编译并运行应用程序以确保业务逻辑按要求运行。
4 最后,以 war 文件的形式压缩应用程序 war 文件夹的内容,并将其部署在 Apache Tomcat Web 服务器中。
5 按照最后一步中的说明,使用适当的 URL 启动您的 Web 应用程序。

以下是修改后的模块描述符 src/com.tutorialspoint/HelloWorld.gwt.xml 的内容。

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>

   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.tutorialspoint.client.HelloWorld'/>

   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>

</module>

以下是修改后的样式表文件war/HelloWorld.css的内容。

body {
   text-align: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

以下是修改后的 HTML 主机文件 war/HelloWorld.html 的内容。

<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <h1>Hello World</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

我对上一个示例的 HTML 做了一些修改。在这里我创建了一个占位符 <div>...</div>,我们将使用入口点 java 类在其中插入一些内容。因此,让我们拥有 Java 文件 src/com.tutorialspoint/HelloWorld.java 的以下内容。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      HTML html = new HTML("<p>Welcome to GWT application</p>");
      
      RootPanel.get("gwtContainer").add(html);
   }
}

我们在这里创建了基本的小部件 HTML,并将其添加到 id="gwtContainer" 的 div 标签内。我们将在接下来的章节中研究不同的 GWT 小部件。

完成所有更改后,让我们像在 GWT - 创建应用程序 一章中所做的那样,在开发模式下编译并运行应用程序。如果您的应用程序一切正常,则将产生以下结果 −

GWT Application Result2

创建 WAR 文件

现在我们的应用程序运行良好,我们准备将其导出为 war 文件。

按照以下步骤 −

  • 进入项目的 war 目录 C:\workspace\HelloWorld\war

  • 选择 war 目录中可用的所有文件和文件夹。

  • 压缩所有选定的文件和文件夹文件夹中的一个名为 HelloWorld.zip 的文件。

  • HelloWorld.zip 重命名为 HelloWorld.war

部署 WAR 文件

  • 停止 tomcat 服务器。

  • HelloWorld.war 文件复制到 tomcat 安装目录 > webapps 文件夹。

  • 启动 tomcat 服务器。

  • 查看 webapps 目录,应该有一个文件夹 helloworld 被创建。

  • 现在 HelloWorld.war 已成功部署在 Tomcat Webserver 根目录中。

运行应用程序

在 Web 浏览器中输入 URL:http://localhost:8080/HelloWorld 以启动应用程序

服务器名称 (localhost) 和端口 (8080) 可能因您的 tomcat 配置而异。

GWT Application Result3