验证GWT

时间:2018-03-15 19:45:35

标签: java gwt

我正在查看我必须处理的代码。基本上我必须为按钮的监听器添加验证。 代码已经有多个验证。他们有点像一个级联。 按钮的监听器调用asyncCallBack方法,如果一切正常,则在方法的onsuccess部分调用下一个,下一个的那个,直到它到达结束并转到下一页。我不是这种方法的粉丝,因为它有点混乱。使用最佳实践的最佳方法是什么。

代码示例:

Button btnOK = new Button("Aceptar");
btnOK.addListener(Events.Select, new Listener<ButtonEvent>() {
public void handleEvent(ButtonEvent e) {
    myService.getInfo1(1, txt, "N",
        new AsyncCallback<List<InfoService>>() {
            public void onFailure(Throwable caught) {
            // goes back
                return
            }

            public void  onSuccess(
                List<Object> result) {
                    // do some validation with the result
                    validation2();
                }

        }
    }
}

public void validation2(){
    myService.getDireccionCanalesElectronicos(id, new AsyncCallback<MyResult>() {
        public void onSuccess(MyResult result) {
            // do some validation with the result
            validation3();
        }
        ...
    }
}

public void validation3(){
    myService.getDireccionCanalesElectronicos(id, new AsyncCallback<MyResult>() {
        public void onSuccess(MyResult result) {
            // do some validation with the result
            validation4();
        }
        ...
    }
}

有没有更好的方法来做到这一点,它似乎很麻烦,很难遵循。添加另一个验证很复杂。这看起来不是一个好习惯。

2 个答案:

答案 0 :(得分:2)

在servlet中创建一个调用所有验证方法并在客户端只进行一次调用的方法吗?

public void validation()
{
   boolean ok = validation1();
  if (ok) ok = validation2();

  return validation;
}

答案 1 :(得分:1)

使用mirco服务有时很难处理。正如@Knarf所说,这是一种方法。但有时您可能想要在客户端处理呼叫。另一个将使用这个小框架:sema4g。它可以帮助您解决问题。

解决方案可能如下:

首先创建sem4g命令:

private SeMa4gCommand createGetInfoCommand() {
  return new AsyncCommand() {
    // create callback
    MethodCallbackProxy<List<InfoService>> proxy = new MethodCallbackProxy<List<InfoService>>(this) {
      @Override
      protected void onProxyFailure(Method method,
                                    Throwable caught) {
        // Enter here the code, that will
        // be executed in case of failure
      }

      @Override
      protected void onProxySuccess(Method method,
                                    List<InfoService> response) {
        // Enter here the code, that will
        // be executed in case of success
      }
    };

    @Override
    public void execute() {
      // That's the place for the server call ...
      myService.getInfo1(1, txt, "N", proxy);
    }
  };
}

为您的所有电话执行此操作;

private SeMa4gCommand createCommandGetDireccionCanalesElectronicos() {
  return new AsyncCommand() {
    // create callback
    MethodCallbackProxy<MyResult> proxy = new MethodCallbackProxy<MyResult>(this) {
      @Override
      protected void onProxyFailure(Method method,
                                    Throwable caught) {
        // Enter here the code, that will
        // be executed in case of failure
      }

      @Override
      protected void onProxySuccess(Method method,
                                    List<MyResult>  response) {
        // Enter here the code, that will
        // be executed in case of success
      }
    };

    @Override
    public void execute() {
      // That's the place for the server call ...
      myService. getDireccionCanalesElectronicos(id, proxy);
    }
  };
}

为所有呼叫完成此操作后,创建一个sema4g上下文并运行它:

  try {
    SeMa4g.builder()
      .addInitCommand(new InitCommand() {
                            @Override
                            public void onStart() {
                              // Enter here your code, that
                              // should be executed when
                              // the context is started
                            })
      .addFinalCommand(new FinalCommand() {
                             @Override
                             public void onSuccess() {
                               // Enter here the code, that will
                               // be executed in case the context
                               // ended without error
                             }

                             @Override
                              public void onFailure() {
                                // Enter here the code, that will
                                // be executed in case the context
                                // ended with an error
                              })
      .add(createGetInfoCommand())
      .add(createCommandGetDireccionCanalesElectronicos())
      .build()
      .run();
  } catch (SeMa4gException e) {
    // Ups, something wrong with the context ...
  }

有关更多信息,请阅读文档。如果您有任何疑问,请随时提出:SeMa4g Gitter room

希望有所帮助。