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

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

最近在学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 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方法。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮