Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4112869
  • 博文数量: 626
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 11080
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-23 13:08
文章分类

全部博文(626)

文章存档

2015年(72)

2014年(48)

2013年(506)

分类: JavaScript

2013-09-23 09:41:30

GWT in Action (1)


Chapter I
The difference between GWT and all those other frameworks is that with GWT you write your browser-side code in Java instead of JavaScript.
GWT allows you to interact with existing JavaScript code.
At the core of GWT is a Java-to-JavaScript compiler that produces code capable of running on Internet Explorer, Firefox, Mozilla, Safari and Opera.
The compiler converts the Java syntax to JavaScript, utilizing JavaScript versions of commonly used Java classes like vector, HashMap and Date.
GWT also includes a large library of widgets and panels, making it effortless to build a web application that looks more like a desktop application.
The widget library includes the usual suspects like text boxes, dropdown menus, and other form fields. In addition, it includes complex widgets including a menu bar, tree control, dialog box, tab panel, stack panel, and others.
When it comes to communication with the server, GWT has a tool for every job. First, it includes several wrappers of varying complexity and capability around the JavaScript XMLHttpRequest object, and object often  associated with Asynchronous JavaScript + XML(Ajax) development. Another tool provided by GWT is a set of classes fro supporting the JavaScript Object Notation(JSON) message format. GWT also provides some of its own special sauce in the form of a tool that lets you send Java objects between the browser and server without the need to translate them into an intermediate message format.
JSON is a popular message format known for its simplicity and widespread availability.
These tools for communication allow you to access server-side services written in any language, and make it possible to integrate with frameworks such as Java Server Faces(JSF), Spring, Struts, and Enterprise JavaBeans(EJBs).
Toward this end, GWT provides support for the JUnit testing framework and a specialhosted-mode browser that lets you develop and bebug in Java without ever needing to deploy your code to a server.
GWT provides a rich set of tools focused on solving the problem of moving the desktop application into the browser, including a rich set of widgets and many other tools. The GWT toolbox provides an XML parser, several tools for communicating with the server, internationalization and configuration tools, and a browser-history management system.
The GWT compiler's responsibility is to convert your Java code into JavaScript code, in much the same way the Java compiler compiles your Java code into bytecode. You compile your project by running the Java program com.google.gwt.dev.GWTCompiler, passing it the location of your module definition file along with some other parameters.
A module is a set of related Java classes and files accompanied by a single configuration file. The module definition typically includes an entry point, which is a class that executes when the application starts.
The compiler starts with the entry-point class, following dependencies required to compile the Java code.
The compiler has three style modes that determine what the resulting JavaScript looks like.
The default style is obfuscate, which makes the JavaScript look like alphabet soup. Everything is compressed and nearly impossible to decipher.
--------------------------------------------------
function b(){return ();}
--------------------------------------------------
The next style is pretty, which generates readable JavaScript.
--------------------------------------------------
function _toString(){
  return ();
}
--------------------------------------------------
The last style is detailed, which produces JavaScript code that looks like the pretty style with the addition of the full class name as part of the JavaScript method name. This makes it easy to trace the JavaScript code back to the originating Java code.
--------------------------------------------------
function java_long_Object_toString__(){
  return __();
}
--------------------------------------------------
The 'pretty' and 'detailed' styles are typically used only during development so that JavaScript errors in your browser are easier to track back to the source Java code. 
Another important aspect of the compiler is that it compiles from Java source code, mot compiled Java binaries. This plays a role when you want to distribute GWT code for reuse.
The GWT compiler also requires that the source code be compliant with the Java 1.4 syntax.
One last feature to note is that when your code is compiled to JavaScript, it results in a different JavaScript file for each browser type and target locale. Typically, this means your application will be compiled to a minimum of four or five separate JavaScript files. Each of these files is meant to run on a specific browser type, version, and locale. A bootstrap script, initially loaded by the browser, automatically pulls the correct file when the application is loaded.


1) Classes from java.lang.* that are available in GWT
2) Classes from java.util.* that are available in GWT
You need to notice some additional differences.
Some of the functionality differs from the JRE versions in subtle ways. 
(1) Double and Float should not be used as HashMap keys for performance reasons.
(2) For String.replaceAll, String.replaceFirst, and String.split, the regular expressions vary from the standard Java implementation.
(3) SringBuffer(int) behaves the same as StringBuffer().
(4) System.out and System.err are available but have no functionality in web mode.
(5) The stack-trace-related methods in Throwable aren't functional due to the lack of stack-trace support.
(6) The implementation of the Vector class doesn't include any of the capacity and growth-management functionality of the normal Java implementation, nor is there any checking of index validity.
Generally speaking, GWT uses three types of components: widgets, panels for layout, and interactive panels. 

阅读(2251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~