GWT RPC - 为什么数据库的结果打印两次?

时间:2011-12-20 01:02:22

标签: gwt rpc

我正在编写一个简单的应用程序,将用户输入数据库&显示在Eclipse中使用GWT RPC,Hibernate的用户列表。我得到的问题是用户列表打印两次。

这是我的代码,我在其中调用insert user&显示用户列表方法。

package rpctest.client;

import java.util.ArrayList;

import rpctest.shared.User;
import rpctest.shared.FieldVerifier;
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.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Rpctest implements EntryPoint {

    final TextBox firstName = new TextBox();
    final TextBox lastName = new TextBox();
    final Button ans = new Button("Add User");
    //final Label label1 = new Label("First Name");
    //final Label label2 = new Label("Last Name");
    private FlexTable userFlexTable = new FlexTable();
    //final Label errorLabel = new Label();

    private VerticalPanel mainpanel = new VerticalPanel();
    private HorizontalPanel addpanel1 = new HorizontalPanel();
    private HorizontalPanel addpanel2 = new HorizontalPanel();
    private final RpctestServiceAsync callService = GWT
            .create(RpctestService.class);

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {

        userFlexTable.setText(0, 0, "User ID");
        userFlexTable.setText(0, 1, "First Name");
        userFlexTable.setText(0, 2, "Second Name");
        userFlexTable.setText(0, 3, "Remove");

        //add input boxes to panel
        addpanel1.add(firstName);
        addpanel1.add(lastName);

        firstName.setFocus(true);

        //add input/result panels 
        mainpanel.add(userFlexTable);
        mainpanel.add(addpanel1);
        addpanel1.add(ans);

        ans.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                        addStock();                 
            }
        });

        lastName.addKeyPressHandler(new KeyPressHandler() {
              public void onKeyPress(KeyPressEvent event) {
                  if (event.getCharCode() == KeyCodes.KEY_ENTER) {
                      addStock();
                  }
                }
              });

        RootPanel.get().add(mainpanel);
        getUser();
    }

private void addStock(){

        String name1 = firstName.getValue();
        // Stock code must be between 1 and 10 chars that are numbers, letters, or dots.
        /*if (!name1.matches("^[0-9A-Z\\.]{1,10}$")) {
          Window.alert("'" + name1 + "' is not a valid name.");
          firstName.selectAll();
          return;
        }*/
         firstName.setValue("");

        String name2 = lastName.getValue();
        /*if (!name2.matches("^[0-9A-Z\\.]{1,10}$")) {
              Window.alert("'" + name1 + "' is not a valid name.");
              lastName.selectAll();
              return;
            }*/
        lastName.setValue("");
        firstName.setFocus(true);

        callService.addUser(name1,name2,
            new AsyncCallback<String>() {
            public void onFailure(Throwable caught) {
                // Show the RPC error message to the user
                    Window.alert("check your inputs");
                }

            @Override
            public void onSuccess(String result) {
            // TODO Auto-generated method stub
                // Add the user to the table.
              //  int row = userFlexTable.getRowCount();
              //  userFlexTable.setText(row, 1, result);
               getUser();
            }
        });
    }

private void getUser(){

    callService.getUser(new AsyncCallback<User[]>() {
            public void onFailure(Throwable caught) {
                // Show the RPC error message to the user
                    Window.alert("Problem in database connection");
                }

            @Override
            public void onSuccess(User[] result) {
                // TODO Auto-generated method stub
                for(int i = 0; i < result.length; i ++)
                    {
                     //String s = result[i].getFirstName();               
                     int row = userFlexTable.getRowCount();
                     userFlexTable.setText(row, 0, result[i].getId().toString());
                     userFlexTable.setText(row, 1, result[i].getFirstName());
                     userFlexTable.setText(row, 2, result[i].getLastName());
                        }                       

            }
        });

      }
}

2 个答案:

答案 0 :(得分:0)

如果输入的名称有效并且对服务的调用成功,你是否注意到你正在调用getUser两次?

你必须删除其中一个!

答案 1 :(得分:0)

每个新条目都会调用getUser。所有数据再次输入表格。