一.为什么我们选择GWT?
Google 网页工具包 (GWT),可以使用 Java 编程语言编写 AJAX 前端,然后 GWT 会交叉编译到优化的 JavaScript 中,而 JavaScript 可以自动在所有主要浏览器上运行。在开发过程中,您可以用 JavaScript 按照您习惯的相同“编辑 - 刷新 - 查看”循环快速反复,还有另一个好处就是能够调试和逐行单步调试 Java 代码。准备好进行部署后,GWT 会将 Java 源代码编译到优化且独立的 JavaScript 文件中。使用 Google 网页工具包可以轻松地为现有网页或整个应用程序构建一个 Widget。
使用 Java 语言编写 AJAX 应用程序,然后编译为优化的 JavaScript
与仅在文本级别运行的 JavaScript Minifier 不同,GWT 编译器会在整个 GWT 代码库中执行综合性静态分析和优化,通常生成的 JavaScript 加载和执行均比等效手写的 JavaScript 更快。例如,GWT 编译器可以安全地消除无用代码 -- 极大的减少不使用的类、方法、字段甚至方法参数 -- 以确保您编译的脚本尽可能最小。另一个示例:GWT 编译器选择性地内联方法,消除方法调用的性能开销。
交叉编译提供了开发所需的可维护的抽象方法和模块性,而不会导致运行时性能损失。
二.为什么会选择SmartGWT?
SmartGWT是一个利用GWT封装SmartClient的开源项目。类似于GWT-Ext。SmartClient是一个企业级的ajax框架,包括非常出色的UI库、工具库和客户端服务端数据绑定等功能。
看看它的Dome,你就会为它的功能所折服的。
http://www.smartclient.com/smartgwt/showcase/
三.GWT与SmartGWT整合
1.下载SmartGWT2.0,在这里,http://code.google.com/p/smartgwt/downloads/list
解压,里面包括smartgwt.jar,smartgwt-skins.jar两个jar文件,API以及几个dome。
2.假定你已经配置好Eclipse下开发GWT的环境,如果你还没有配置好,请看这里:
http://code.google.com/webtoolkit/usingeclipse.html
3.在Eclipse下,新建项目firstSmartGWT,包名:com.mycompany.app
可以看到它已经给你生成了一个helloworld的例子。
4.引入smartGWT相关的jar包,右击项目,build path,add external Archives,如后选择smartgwt.jar,smartgwt-skins.jar引入。
5.修改FirstSmartGWT.html
在war包下面,打开FirstSmartGWT.html,添加SmartGWT的资源引用模块(红色部分),其中firstsmartgwt为项目名的全部小写名称。
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <!-- The HTML 4.01 Transitional DOCTYPE declaration-->
- <!-- above set at the top of the file will set -->
- <!-- the browser's rendering engine into -->
- <!-- "Quirks Mode". Replacing this declaration -->
- <!-- with a "Standards Mode" doctype is supported, -->
- <!-- but may lead to some differences in layout. -->
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <!-- -->
- <!-- Consider inlining CSS to reduce the number of requested files -->
- <!-- -->
- <link type="text/css" rel="stylesheet" href="FirstSmartGWT.css">
- <span style="color: #ff0000;"> <script> var isomorphicDir = "firstsmartgwt/sc/"; </script></span>
- <!-- -->
- <!-- Any title is fine -->
- <!-- -->
- <title>GWT和SmartGWT整合实例</title>
- <!-- -->
- <!-- This script loads your compiled module. -->
- <!-- If you add any GWT meta tags, they must -->
- <!-- be added before this line. -->
- <!-- -->
- <script type="text/javascript" language="javascript" src="firstsmartgwt/firstsmartgwt.nocache.js"></script>
- </head>
- <!-- -->
- <!-- The body can have arbitrary html, or -->
- <!-- you can leave the body empty if you want -->
- <!-- to create a completely dynamic UI. -->
- <!-- -->
- <body>
- <span style="white-space: pre;"> </span>
- <!-- OPTIONAL: include this if you want history support -->
- <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
- <!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
- <noscript>
- <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
- Your web browser must have JavaScript enabled
- in order for this application to display correctly.
- </div>
- </noscript>
- <span style="color: #00ff00;"><h1>Web Application Starter Project</h1>
- <table align="center">
- <tr>
- <td colspan="2" style="font-weight:bold;">Please enter your name:</td>
- </tr>
- <tr>
- <td id="nameFieldContainer"></td>
- <td id="sendButtonContainer"></td>
- </tr>
- </table></span>
- </body>
- </html>
下面的为helloworld相关的内容,删掉。
- <h1>Web Application Starter Project</h1>
- <table align="center">
- <tr>
- <td colspan="2" style="font-weight:bold;">Please enter your name:</td>
- </tr>
- <tr>
- <td id="nameFieldContainer"></td>
- <td id="sendButtonContainer"></td>
- </tr>
- </table>
6.修改web.xml
在war/WEB-INF下面打开web.xml。
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <span style="color: #00ff00;"><!-- Servlets -->
- <servlet>
- <servlet-name>greetServlet</servlet-name>
- <servlet-class>com.mycompany.app.server.GreetingServiceImpl</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>greetServlet</servlet-name>
- <url-pattern>/firstsmartgwt/greet</url-pattern>
- </servlet-mapping></span>
- <!-- Default page to serve -->
- <welcome-file-list>
- <welcome-file>FirstSmartGWT.html</welcome-file>
- </welcome-file-list>
- </web-app>
删掉下面的内容
- <!-- Servlets -->
- <servlet>
- <servlet-name>greetServlet</servlet-name>
- <servlet-class>com.mycompany.app.server.GreetingServiceImpl</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>greetServlet</servlet-name>
- <url-pattern>/firstsmartgwt/greet</url-pattern>
- </servlet-mapping>
7.修改FirstSmartGWT.gwt.xml
在src/com/mycompany/app下面,添加<inherits name="com.smartgwt.SmartGwt"/>,引用SmartGWT模块
- <?xml version="1.0" encoding="UTF-8"?>
- <module rename-to='firstsmartgwt'>
- <!-- Inherit the core Web Toolkit stuff. -->
- <inherits name='com.google.gwt.user.User'/>
- <!-- Inherit the default GWT style sheet. You can change -->
- <!-- the theme of your GWT application by uncommenting -->
- <!-- any one of the following lines. -->
- <inherits name='com.google.gwt.user.theme.standard.Standard'/>
- <span style="color: #ff0000;"><inherits name="com.smartgwt.SmartGwt"/></span>
- <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
- <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
- <!-- Other module inherits -->
- <!-- Specify the app entry point class. -->
- <entry-point class='com.mycompany.app.client.FirstSmartGWT'/>
- <!-- Specify the paths for translatable code -->
- <source path='client'/>
- </module>
8.修改FirstSmartGWT.java
打开src/com/mycompany/client下面的FirstSmartGWT.java,用下面的内容替换掉以前的内容。
- package com.mycompany.app.client;
- import com.google.gwt.core.client.EntryPoint;
- import com.smartgwt.client.util.SC;
- import com.smartgwt.client.widgets.IButton;
- import com.smartgwt.client.widgets.events.ClickEvent;
- import com.smartgwt.client.widgets.events.ClickHandler;
- /**
- * Entry point classes define <code>onModuleLoad()</code>.
- */
- public class FirstSmartGWT implements EntryPoint {
- /**
- * This is the entry point method.
- */
- public void onModuleLoad() {
- IButton button = new IButton("点我");
- button.addClickHandler(new ClickHandler() {
- @Override
- public void onClick(ClickEvent event) {
- // TODO Auto-generated method stub
- SC.say("恭喜你,GWT和SmartGWT集成好了。");
- }
- });
- button.draw();
- }
- }
9.至此GWT和smartGWT集成完毕。