Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GWT RPC-为什么来自数据库的结果会打印两次?_Gwt_Rpc - Fatal编程技术网

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

GWT RPC-为什么来自数据库的结果会打印两次?,gwt,rpc,Gwt,Rpc,我正在编写一个简单的应用程序,将一个用户输入数据库&使用GWT-RPC和Eclipse中的Hibernate显示用户列表。我遇到的问题是,用户列表打印了两次 这是我的代码,我在其中调用insert user和display users list方法 package rpctest.client; import java.util.ArrayList; import rpctest.shared.User; import rpctest.shared.FieldVerifier; import

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

这是我的代码,我在其中调用insert user和display users list方法

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());
                        }                       

            }
        });

      }
}

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


你必须移除其中一个

getUser在每个新条目上都被调用&所有数据都被再次输入到表中

数据在数据库中是唯一的吗?在UI中,您是否看到相同的Id两次?是的,所有值打印两次,如1,2,3,4&再次打印1,2,3,4我输入的最后一个值打印一次,之前所有两次前面两次都是这样&最后一次我认为这不是问题,因为我认为onModuleLoad不会被调用两次。我将getUser调用放在onModuleLoad方法中,这样在应用程序开始构建UI时,它也会显示所有数据稍后,当insert调用成功时,再次调用getUser,并用新条目填充表格。删除其中任何一个调用都将停止一个函数。事实上,我找到了原因&在每个新条目上调用getUser&所有数据都再次输入到表中。我每次只需要检查并用新条目填充flex表。谢谢你的回复。是的,你是对的。对不起,我的错,没有注意到电话的位置。很高兴你找到了解决办法