#眉標=AJAX #副標=AJAX開發工具(5) #大標=GWT結合Web Services開發AJAX應用程式 #作者=文/沈炳宏 ==程式1 =========== XFireServlet XFire Servlet org.codehaus.xfire.transport.http. XFireConfigurableServlet XFireServlet /servlet/XFireServlet/* XFireServlet /services/* ================ ==程式2 ===========   private String title; private String isbn; private String author; public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } ================ ==程式3 =========== public Book[] getBooks(); public Book findBook(String isbn); public Map getBooksMap(); ================ ==程式4 =========== public class BookServiceImpl    implements BookService   {   private Book onlyBook; public BookServiceImpl() { onlyBook = new Book(); onlyBook.setAuthor("Bill Shen"); onlyBook.setTitle("RUN!PC"); onlyBook.setIsbn("0123456789"); } public Book[] getBooks() { return new Book[] { onlyBook }; } public Book findBook(String isbn) { if (isbn.equals(onlyBook.getIsbn())) return onlyBook; return null; } public Map getBooksMap() { Map result = new HashMap(); result.put(onlyBook.getIsbn(), onlyBook); return result; } } ================ ==程式5 =========== BookService http://runpc.com/BookService com.runpc.BookService com.runpc.BookServiceImpl ================ ==程式6 =========== public class BookServiceTest extends AbstractXFireAegisTest { public void setUp() throws Exception { super.setUp(); XFire xfire = getXFire(); ServiceFactory factory = new ObjectServiceFactory( xfire.getTransportManager(), null); Service service = factory.create(BookService.class); service.setProperty( ObjectInvoker.SERVICE_IMPL_CLASS, BookServiceImpl.class); xfire.getServiceRegistry().register(service); } public void testClient() throws Exception { Service serviceModel = getXFire().getServiceRegistry(). getService("BookService"); XFireProxyFactory serviceFactory = new XFireProxyFactory(getXFire()); BookService service = (BookService) serviceFactory.create(serviceModel, "xfire.local://BookService"); assertNotNull(service); Book[] books = service.getBooks();     assertNotNull(books); for (int i = 0 ; i < books.length; i++){ System.out.println(books[0].getAuthor()); System.out.println(books[0].getTitle()); System.out.println(books[0].getIsbn()); } } } ================ ==程式7 =========== public interface StockService extends com.google.gwt.user.client.rpc.RemoteService { public String getStock(String message); } ================ ==程式8 =========== public interface StockServiceAsync { public void getStock(String message, AsyncCallback callback); } ================ ] ==程式9 =========== public class StockServiceImpl extends RemoteServiceServlet implements StockService { public String getStock(String message) { StockQuoteClient stockQuoteClient = new StockQuoteClient(); StockQuoteSoap stockQuoteSoap = stockQuoteClient.getStockQuoteSoap(); String result = ""; MySAXParserBean p = new MySAXParserBean(); try { Collection v = p.parse(stockQuoteSoap.getQuote(message)); Iterator it = v.iterator(); while (it.hasNext()) { MyElement element = (MyElement) it.next(); String tag = element.getQname(); if (tag.equals("Symbol")) { result += "股票代碼: " + element.getValue() + "\r\n"; … } } } catch (Exception e) { e.printStackTrace(); } return result; } } ================ ==程式10 =========== GWT With Web Services DEMO

RUN!PC Web Services Demo : Stock & Weather

================ ==程式11 =========== ================ ==程式12 =========== public class WSEntry implements EntryPoint { final Button button = new Button("查詢股價"); final Label label = new Label(); final TextBox textBox = new TextBox(); final ListBox stockListbox = new ListBox(); private StockTimer stockTimer; public void onModuleLoad() { stockListbox.addItem("5秒", "5"); stockListbox.addItem("20秒", "20"); stockListbox.addItem("60秒", "60"); textBox.setText("GOOG"); button.addClickListener(new ClickListener() { public void onClick(Widget sender) { … Timer stockClickTimer = new Timer() { public void run() { helloService.getStock(textBox.getText(), new AsyncCallback() { public void onSuccess(Object result) { label.setText((String) result); } public void onFailure(Throwable caught) { label.setText( "無法取得股價資訊,請檢查網路連線!"); }}); }}; stockClickTimer.scheduleRepeating( Integer.parseInt(stockListbox.getValue( stockListbox.getSelectedIndex())) * 1000); } }); … } } ================ ==程式13 =========== public interface WeatherService extends com.google.gwt.user.client.rpc.RemoteService{ public String getWeather(String country,String city); public String getCities(String country); } ================ ==程式14 =========== public interface WeatherServiceAsync { public void getWeather( String country,String city,AsyncCallback callback); public void getCities( String country,AsyncCallback callback); } ================ ==程式15 =========== public class WeatherServiceImpl extends RemoteServiceServlet implements WeatherService { public String getWeather(String country, String city) { GlobalWeatherClient globalWeatherClient = new GlobalWeatherClient(); GlobalWeatherSoap globalWeatherSoap = globalWeatherClient.getGlobalWeatherSoap(); ArrayList cityList = new ArrayList(); MySAXParserBean p = new MySAXParserBean(); String weather = ""; try { Collection v = p.parse( globalWeatherSoap.getWeather(city, country)); Iterator it = v.iterator(); while (it.hasNext()) { MyElement element = (MyElement) it.next(); String tag = element.getQname(); if (tag.equals("Location")) { weather += "位置: " + element.getValue() + "\r\n"; … } return weather; } public String getCities(String country) { GlobalWeatherClient globalWeatherClient = new GlobalWeatherClient(); GlobalWeatherSoap globalWeatherSoap = globalWeatherClient.getGlobalWeatherSoap(); ArrayList cityList = new ArrayList(); MySAXParserBean p = new MySAXParserBean(); try { Collection v = p.parse(globalWeatherSoap .getCitiesByCountry(country)); Iterator it = v.iterator(); while (it.hasNext()) { MyElement element = (MyElement) it.next(); String tag = element.getQname(); if (tag.equals("City")) { cityList.add(element.getValue()); }}} catch (Exception e) { e.printStackTrace(); } String cities = ""; for (Iterator it = cityList.iterator(); it.hasNext();){ cities += (String)it.next() + ","; } return cities; } } ================ ==程式16 =========== final Button queryCityButton = new Button("查詢都市列表"); final Button queryWeatherButton = new Button("查詢都市天氣"); final Label weatherlabel = new Label(); final TextBox weatherCitytextBox = new TextBox(); weatherCitytextBox.setText("Taiwan"); queryCityButton.addClickListener( new ClickListener() { public void onClick(Widget sender) { … weatherService.getCities( weatherCitytextBox.getText(), new AsyncCallback() { public void onSuccess(Object result) { String cityList = (String)result; String[] tmp = cityList.split(","); for (int i = 0 ; i < tmp.length; i++){ cityListbox.addItem(tmp[i]); } RootPanel.get("CountryInfo").add(cityListbox); RootPanel.get("WeatherButton"). add(queryWeatherButton); } public void onFailure(Throwable caught) { weatherlabel.setText( "無法取得天氣資訊,請檢查網路連線!"); }});}}); queryWeatherButton.addClickListener( new ClickListener() { public void onClick(Widget sender) { weatherlabel.setText(""); … weatherService.getWeather( weatherCitytextBox.getText(), cityListbox.getItemText( cityListbox.getSelectedIndex()), new AsyncCallback() { public void onSuccess(Object result) { weatherlabel.setText((String)result); RootPanel.get("WeatherInfo").add(weatherlabel); } public void onFailure(Throwable caught) { weatherlabel.setText( "無法取得天氣資訊,請檢查網路連線!"); }}); }}); … RootPanel.get("CityButton"). add(queryCityButton); ================