blent
blent
2011-12-29 20:19
采纳率: 25%
浏览 274
已采纳

帮忙看一下这些代码有问题啊

最近在学GWT,因为要用到一个包,所以接触到这个例子但有些不明白,这些代码是

[code="java"]package com.google.code.gwt.database.sample.hellodatabase.client;

import java.util.Date;
import java.util.List;

import com.google.code.gwt.database.client.Database;
import com.google.code.gwt.database.client.service.DataServiceException;
import com.google.code.gwt.database.client.service.ListCallback;
import com.google.code.gwt.database.client.service.RowIdListCallback;
import com.google.code.gwt.database.client.service.ScalarCallback;
import com.google.code.gwt.database.client.service.VoidCallback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**

  • Entry point classes define onModuleLoad().
    */
    public class HelloDatabase implements EntryPoint {

    ClickCountDataService dbService = GWT.create(ClickCountDataService.class);
    private VerticalPanel vPanel;

    /**

    • This is the entry point method. */ public void onModuleLoad() { if (!Database.isSupported()) { Window.alert("HTML 5 Database is NOT supported in this browser!"); return; }

    // Create the dialog box
    final DialogBox dialogBox = new DialogBox();
    dialogBox.setText("Welcome to GWT Database Demo!");
    dialogBox.setAnimationEnabled(true);
    Button closeButton = new Button("close", new ClickHandler() {
    public void onClick(ClickEvent event) {
    dialogBox.hide();
    }
    });
    VerticalPanel dialogVPanel = new VerticalPanel();
    dialogVPanel.setWidth("100%");
    dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
    final VerticalPanel clickedData = new VerticalPanel();
    dialogVPanel.add(clickedData);
    dialogVPanel.add(closeButton);

    dialogBox.setWidget(dialogVPanel);

    Image img = new Image("http://code.google.com/webtoolkit/logo-185x175.png");
    Button addClickButton = new Button("Add Click", new ClickHandler() {
    public void onClick(ClickEvent event) {
    dbService.insertClick(new Date(), new RowIdListCallback() {
    public void onFailure(DataServiceException error) {
    Window.alert("Failed to add click! " + error);
    }

      public void onSuccess(final List<Integer> rowIds) {
        dbService.getClicks(new ListCallback<ClickRow>() {
          public void onFailure(DataServiceException error) {
            Window.alert("Failed to query clicks! " + error);
          }
    
          public void onSuccess(List<ClickRow> result) {
            clickedData.clear();
            clickedData.add(new Label("Last click insert ID: "
                + rowIds.get(0)));
            for (ClickRow row : result) {
              clickedData.add(new Label("Clicked on " + row.getClicked()));
            }
            dialogBox.center();
            dialogBox.show();
          }
        });
      }
    });
    

    }
    });
    Button getCountButton = new Button("Get Counts", new ClickHandler() {
    public void onClick(ClickEvent event) {
    getCount();
    }
    });

    vPanel = new VerticalPanel();
    // We can add style names.
    vPanel.addStyleName("widePanel");
    vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
    vPanel.add(img);
    vPanel.add(addClickButton);
    vPanel.add(getCountButton);

    // Add image and button to the RootPanel
    RootPanel.get().add(vPanel);

    // Create table 'clickcount' if it doesn't exist already:
    dbService.initTable(new VoidCallback() {
    public void onFailure(DataServiceException error) {
    Window.alert("Failed to initialize table! " + error);
    }

    public void onSuccess() {
    Window.alert("Database initialized successfully.");
    getCount();
    }
    });

    getVersion();
    }

    private void getVersion() {
    dbService.getSqliteVersion(new ScalarCallback() {
    public void onFailure(DataServiceException error) {
    Window.alert("Failed to get SQLite version! " + error);
    }

    public void onSuccess(String result) {
    vPanel.add(new Label("SQLite version: " + result));
    }
    });
    }

    private void getCount() {
    dbService.getClickCount(new ScalarCallback() {
    public void onFailure(DataServiceException error) {
    Window.alert("Failed to get count! " + error);
    }

    public void onSuccess(Integer result) {
    vPanel.add(new Label("There are " + result + " recorded clicks."));
    }
    });
    }
    }
    [/code]

[code="java"]package com.google.code.gwt.database.sample.hellodatabase.client;

import java.util.Date;

import com.google.code.gwt.database.client.service.Connection;
import com.google.code.gwt.database.client.service.DataService;
import com.google.code.gwt.database.client.service.ListCallback;
import com.google.code.gwt.database.client.service.RowIdListCallback;
import com.google.code.gwt.database.client.service.ScalarCallback;
import com.google.code.gwt.database.client.service.Select;
import com.google.code.gwt.database.client.service.Update;
import com.google.code.gwt.database.client.service.VoidCallback;

/**

  • Demo database service to the 'ClckCnt' database.
  • @author bguijt
    */
    @Connection(name="ClckCnt", version="1.0",
    description="Click Counter", maxsize=10000)
    public interface ClickCountDataService extends DataService {

    /**

    • Makes sure that the 'clickcount' table exists in the Database. */ @Update("CREATE TABLE IF NOT EXISTS clickcount ("
      • "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
      • "clicked INTEGER)") void initTable(VoidCallback callback);

    /**

    • Records a Click value, and obtains the ID of the inserted record. */ @Update("INSERT INTO clickcount (clicked) VALUES ({when.getTime()})") void insertClick(Date when, RowIdListCallback callback);

    /**

    • Returns all clicks. */ @Select("SELECT clicked FROM clickcount") void getClicks(ListCallback callback);

    /**

    • Obtains the number of clicks recorded in the database. / @Select("SELECT count() FROM clickcount") void getClickCount(ScalarCallback callback);

    /**

    • Obtains the version number of the SQLite database. */ @Select("SELECT sqlite_version()") void getSqliteVersion(ScalarCallback callback); } [/code]

[code="java"]
package com.google.code.gwt.database.sample.hellodatabase.client;

import java.util.Date;

import com.google.gwt.core.client.JavaScriptObject;

/**

  • @author bguijt
    */
    public class ClickRow extends JavaScriptObject {

    protected ClickRow() {}

    /**

    • @return the 'clicked' property (an integer) as a Java Date (SQLite does not support DATE types). / public final native Date getClicked() /-{ var dateClicked = new Date(); dateClicked.setTime(this.clicked); return dateClicked; }-*/; } [/code]

1.请问HelloDatabase类里边的
[code="java"]
Button addClickButton = new Button("Add Click", new ClickHandler() {
public void onClick(ClickEvent event) {
dbService.insertClick(new Date(), new RowIdListCallback() {
public void onFailure(DataServiceException error) {
Window.alert("Failed to add click! " + error);
}

      public void onSuccess(final List<Integer> rowIds) {
        dbService.getClicks(new ListCallback<ClickRow>() {
          public void onFailure(DataServiceException error) {
            Window.alert("Failed to query clicks! " + error);
          }

          public void onSuccess(List<ClickRow> result) {
            clickedData.clear();
            clickedData.add(new Label("Last click insert ID: "
                + rowIds.get(0)));
            for (ClickRow row : result) {
              clickedData.add(new Label("Clicked on " + row.getClicked()));
            }
            dialogBox.center();
            dialogBox.show();
          }
        });
      }
    });
  }
});[/code]

为什么有两个onSuccess方法,而且dbService.insertClick(new Date(), new RowIdListCallback() {。。。})里边的参数都初始化了些什么参数啊。

2.HelloDatabase 类中那个GWT.create()作用是什么,最后生成的是什么对象呢(怎么被初始化的)。
3.ClickCountDataService 里的那些注释怎么起作用的呢?

纠结中,求解啊,,

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • iteye_16709
    iteye_16709 2011-12-29 22:06
    已采纳

    1.问题一:dbService.insertClick(new Date(), new RowIdListCallback() {。。。})
    这个是java里面的匿名函数,没猜错的话是重写了RowIdListCallback其中方法。
    这种方法可以用内部类来代替。
    即:
    [code="java"]
    private class RowIdListCallback{
    public void onFailure(DataServiceException error) {

    Window.alert("Failed to add click! " + error);

    }

      public void onSuccess(final List<Integer> rowIds) {   
        dbService.getClicks(new ListCallback<ClickRow>() {   
          public void onFailure(DataServiceException error) {   
            Window.alert("Failed to query clicks! " + error);   
          }   
    
          public void onSuccess(List<ClickRow> result) {   
            clickedData.clear();   
            clickedData.add(new Label("Last click insert ID: "  
                + rowIds.get(0)));   
            for (ClickRow row : result) {   
              clickedData.add(new Label("Clicked on " + row.getClicked()));   
            }   
            dialogBox.center();   
            dialogBox.show();   
          } 
    

    }
    [/code]
    在使用时:
    [code="java"]
    dbService.insertClick(new Date(), new RowIdListCallback());
    [/code]

    2.
    [code="java"]
    ClickCountDataService dbService = GWT.create(ClickCountDataService.class);

    [/code]
    这句话创建的是一个服务(service),实际是ClickCountDataService实现类的一个远程对象,在服务器端。用来持久化数据。鼠标选中ClickCountDataService然后Ctrl+T就能看到具体实现

    3.ClickCountDataService 里的那些注释怎么起作用的呢?
    那里的注释其实不是一般的注释 而是java中的“元数据”。元数据的起作用是利用了java反射机制的到Method里面有个getAnnotation方法。

    点赞 评论

相关推荐