摘要: |
本文介紹用GWT(Google Web Toolkit)開發第一個Ajax應用。應用效果,將在應用頁面顯示一個按鈕,點擊時候顯示"HelloWorld" |
關鍵字: |
GWT Google Web Toolkit Ajax 入門應用 |
- eclipse
- google web toolkit
- tomcat_5.0.28
- 以上3者均可通過google搜索方便下載到,安裝配置不再詳細介紹
1、創建應用
創建一個gwt項目的目錄,我的就是在d:\gwt_test\helloworld
用gwt的applicationCreator.bat(該批處理文件在gwt安裝目錄下可以看到)創建應用:
applicationCreator.cmd org.jmg.client.HelloWorld成功執行輸出:
Created directory D:\gwttest\helloworld\src(這個目錄包含自動創建的java文件)
Created directory D:\gwttest\helloworld\src\org\jmg
Created directory D:\gwttest\helloworld\src\org\jmg\client
Created directory D:\gwttest\helloworld\src\org\jmg\public
Created file D:\gwttest\helloworld\src\org\jmg\HelloWorld.gwt.xml
Created file D:\gwttest\helloworld\src\org\jmg\public\HelloWorld.html
Created file D:\gwttest\helloworld\src\org\jmg\client\HelloWorld.java
Created file D:\gwttest\helloworld\HelloWorld-shell.cmd(這個命令將會調用gwt內置的服務器來運行應用)
Created file D:\gwttest\helloworld\HelloWorld-compile.cmd(編譯應用,把前邊的java文件編譯成javascript代碼,和其他的附加文件,注意,他不會編譯server端文件)
2、編譯輸出:
執行helloworld下的HelloWorld-compile.cmd
Output will be written into D:\gwttest\helloworld\www\org.jmg.HelloWorld(默認的,gwt會把客戶端代碼文件放到一個名字為完整的應用名稱的目錄)
Copying all files found on public path
Compilation succeeded
編譯成功。
3、準備tomcat:
在tomcat安裝目錄下webapps目錄創建一個gwt文件夾
相應的創建其他的web應用的文件和文件夾:
gwt\WEB-INF
gwt\WEB-INF\classes
gwt\WEB-INF\lib
gwt\WEB-INF\web.xml
web.xml內容如下: <?xml version="1.0" encoding="UTF-8"?> <web-app> </web-app>
4、部署到tomcat:
在gwt目錄創建helloword目錄
復制剛才執行HelloWorld-compile.cmd生成的文件(在www\org.jmg.HelloWorld目錄)到helloworld目錄下,注意,只要這個文件夾中的文件,不要文件夾。
5、運行看效果:
啟動tomcat
訪問http://localhost:8080/gwt/helloworld/HelloWorld.html
頁面上將會顯示
一個標題,一段說明文字,和一個Click Me按鈕
點擊按鈕將會顯示 "HelloWorld"文字