论坛首页 Web前端技术论坛

GWT与SmartGWT应用简单例子

浏览 17569 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-07   最后修改:2010-01-07
GWT

一.为什么我们选择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">
    <script> var isomorphicDir = "firstsmartgwt/sc/"; </script>
    <!--                                           -->
    <!-- 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>
	
    <!-- 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>

    <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>
  </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>
  
  <!-- 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>
  
  <!-- 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'/>
  <inherits name="com.smartgwt.SmartGwt"/>
  <!-- <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集成完毕。

   发表时间:2010-01-09  
功能强大,就是太慢,渲染速度不可忍受的慢
0 请登录后投票
   发表时间:2010-01-11  
风之狐 写道
功能强大,就是太慢,渲染速度不可忍受的慢

你没有优化好吧。我们用着挺快的啊。
0 请登录后投票
   发表时间:2010-01-11  
偶们公司用smartclient快一年了,没用gwt的,挺好用的,像.net,呵呵。
0 请登录后投票
   发表时间:2010-01-15   最后修改:2010-01-15
egmacross 写道
偶们公司用smartclient快一年了,没用gwt的,挺好用的,像.net,呵呵。

你说的是微软的smartclient吧


是比1.2的时候快了不少,在ie下还是慢
0 请登录后投票
   发表时间:2010-03-09  
请问一下 你调试过程中遇到问题了么 ?
我基本也是这么弄的,在调试过程中 无论是chrome还是ie都会卡住 然后死掉
0 请登录后投票
   发表时间:2010-03-09  
sorry  我搞明白了,在第一次请求那个url的时候无论什么浏览器都会卡死
http://127.0.0.1:8888/Demo2.html?gwt.codesvr=127.0.0.1:9997
因为他是在第一次请求的时候才开始编译那些js。 等一会javaw那个进程cpu下去了之后从新访问那个url就好了。
0 请登录后投票
   发表时间:2010-03-10  
是感觉超级慢啊,比gwt-ext慢多了。楼主能说一下如何优化吗
0 请登录后投票
   发表时间:2010-11-28  
lz写的可以 不过我在用myeclipse8.6.1安装gwt插件时出现关于web的所有功能丢失,不知道是怎么问题。
0 请登录后投票
   发表时间:2010-12-23  
调试模式是慢了点,但是Release以后速度还可以吧,完全可以接受
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics