Book Image

ExtGWT Rich Internet Application Cookbook

By : Odili Charles Opute , Oded Nissan
Book Image

ExtGWT Rich Internet Application Cookbook

By: Odili Charles Opute , Oded Nissan

Overview of this book

<p>Get ready to build the next generation Gmail, Facebook, or Meebo, with HTML5 and Server Push, taking advantage of the power and versatility of Java with ExtGWT. Sencha Ext GWT takes GWT to the next level, giving you high-performance widgets, feature-rich templates and layouts, advanced charting, data loaders and stores,&nbsp; accessibility, and much more.<br /><br /><i>ExtGWT Rich Internet Application Cookbook will teach you to quickly build&nbsp; stunning functionality into your own apps with ExtGWT</i>.<br /><br />This is a catalog of practical solutions to get your ExtGWT web app up and running in no time, with tips for persistence and best practices. You begin by playing with panels, windows, and tabs, to learn the essentials. Next, you engage yourself with forms, buttons, toolbars and menus to build on further. Dealing with the UI and the trees will follow to help you make stunning user interfaces. Then you will be taught to work with Listview, Views, and Gridpanels, the more complex problems. The book will then deal with charts, visualization, and drag and drop to take you to the next level. Finally, you will wind up with serialization, persistence, and custom theming. Now, you are an expert!</p>
Table of Contents (22 chapters)
ExtGWT Rich Internet Application Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Event Handling — Making Those GUIs Do Something
Jakarta Commons-FileUpload

A simple example


The following example shows a simple GWT RPC greeting service, which is generated automatically when you create a GWT project.

Define the main service interface

We create an interface that implements RemoteService and define our remote methods in it. We annotate the service with the @RemoteServiceRelativePath annotation, which specifies the path to the service implementation servlet on the server.

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String name) throws IllegalArgumentException;
}

Define the callback version of the service interface

We define the asynchronous version of our interface, by adding our remote methods with the same parameters as they were defined in the service interface, but without a return value. Instead, we add another parameter which is the AsyncCallback class parameterized by our object return type.

In this example, we return String from the greetServer() method so we pass AsyncCallback<String> as the callback class.

import com.google.gwt.user.client.rpc.AsyncCallback;
public interface GreetingServiceAsync {
void greetServer(String input, AsyncCallback<String> callback) throws IllegalArgumentException;
}

Create the service servlet

Now, we create implementation of our service interface. This is the actual service that will be invoked on the server side. Our service implementation needs to extend the RemoteServiceServlet and implement our service method.

import com.bitrunk.gwtrpc.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) throws IllegalArgumentException {
return "Hello, " + input;
}
}

Our service implementation is actually a servlet. It needs to be defined in the web application deployment descriptor web.xml. Note that the URL mapping of the servlet is composed of <web application root>/<service-path>. The <service-path> is the same URL path defined in our service interface using the @RemoteServiceRelativePath annotation.

// web.xml
<web-app>
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>com.bitrunk.gwtrpc.server.GreetingServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/gwtrpc/greet</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>GwtRPC.html</welcome-file>
</welcome-file-list>
</web-app>

Create and use the service proxy

To invoke our RPC service on the client side, we create an instance of our GreetingServiceAsync interface using a call to GWT.create(), passing the service interface. We can now call the asynchronous method and pass it to our callback class.

The callback class contains the onFailure method, which is called in case of an exception and the onSuccess method which is called if the remote call succeeds.

private GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
...
greetingService.greetServer(textToServer,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
// if the called service method throws an exception
// defined in a throws clause then handle it here.
}
public void onSuccess(String result) {
// Show success message to the user or update some UI
}
});

The GWT RPC service knows the URL of the service by the value of the RemoteServiceRelativePath annotation defined in the service interface. That value is appended to a URL which includes the GWT module name. In case we want to invoke a service that belongs to a different GWT module, we will need to explicitly specify the service URL. This can be done using the ServiceDefTarget interface:

ServiceDefTarget endPoint = (ServiceDefTarget) greetService;
endPoint.setServiceEntryPoint(GWT.getHostPageBaseURL() + "/gxtcookbk/greet");