How to prevent DoubleSubmit in a GWT application?

雨燕双飞 提交于 2019-12-19 03:36:37

问题


To clarify what double submit is: When the user clicks on a submit button twice, the server will process the same POST data twice. To avoid this (apart from disabling the button after a single submit), most web frameworks like Struts provide a token mechanism. I am searching for the equivalent of this in GWT.


回答1:


If you want to avoid submitting twice, how about:

boolean processing = false;
button.addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent event) {
    if (!processing) {
      processing = true;
      button.setEnabled(false);
      // makes an RPC call, does something you only want to do once.
      processRequest(new AsyncCallback<String>() {
        @Override
        public void onSuccess(String result) {
          // do stuff
          processing = false;
          button.setEnabled(true);
        });
      });
    }
  }
});

That's the gist of it.




回答2:


This will be helpfull for you -

    final Button btn = new Button("Open");
    btn.addSelectionListener(new SelectionListener<ButtonEvent>() {
        @Override
        public void componentSelected(ButtonEvent ce) {

                    btn.setEnabled(false);

                    openMethod(name, new AsyncCallback<Void>() {

                        public void onFailure(Throwable caught) {
                                btn.setEnabled(true);
                    }
                    public void onSuccess(Void result) {
                        MessageBox.alert(info, "Opened Window", null);
                        btn.setEnabled(true);
                        window.hide();
                    }
                });
        }
    });


来源:https://stackoverflow.com/questions/4549049/how-to-prevent-doublesubmit-in-a-gwt-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!