面向 Java 开发人员的 Ajax: 探索 Google Web Toolkit

http://tech.ddvip.com   2006年11月20日    社区交流

本文详细介绍面向 Java 开发人员的 Ajax: 探索 Google Web Toolkit

  清单 8 显示了WeatherServiceAsync对象的设置,然后给出了fetchWeatherHtm()的实现,这个实现我在前面提到过(请参阅 添加客户端行为):

  清单 8. 使用 RPC 调用远程服务

// Statically configure RPC service
private static WeatherServiceAsync ws =
  (WeatherServiceAsync) GWT.create(WeatherService.class);
static {
  ((ServiceDefTarget) ws).setServiceEntryPoint("ws");
}
/**
* Asynchronously call the weather service and display results
*/
private void fetchWeatherHtml(String zip, boolean isCelsius) {
  // Hide existing weather report
  hideHtml();
  // Call remote service and define callback behavior
  ws.getWeatherHtml(zip, isCelsius, new AsyncCallback() {
   public void onSuccess(Object result) {
     String html = (String) result;
     // Show new weather report
     displayHtml(html);
   }
   public void onFailure(Throwable caught) {
     Window.alert("Error: " + caught.getMessage());
     txBox.setEnabled(true);
    }
  });
}

  对服务的getWeatherHtml()的实际调用实现起来非常简单:使用一个匿名回调句柄类将服务器的响应传递给显示响应的方法即可。

  图 5 显示了应用程序的运行情况,显示了从 Yahoo! 天气 API 检索的天气报告:

  图 5. Weather Reporter 应用程序显示了从 Yahoo! 得到的报告

  

来源:ibm    作者:Philip McCarthy    责编:豆豆技术应用

正在加载评论...