【问题标题】:Unable to catch onFailure() GWT AsyncCallback无法捕获 onFailure() GWT AsyncCallback
【发布时间】:2016-04-16 02:42:09
【问题描述】:

我正在创建一个需要用户身份验证的应用程序。我在尝试登录时遇到了一个问题。当我输入正确的用户名和密码时,会调用 onSuccess 方法。但是当我输入错误或空字段时,不会调用 onFailure() 方法。

我真的很想知道为什么会这样。因为当用户名或密码不正确时,我不想显示某种对话框。

这是 ClickHandler,它从字段中获取用户名和密码:

loginButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            String username = usernameBox.getText();
            String password = passwordBox.getText();
            performUserConnection(username, password);
        }
    });

这是执行用户连接的方法,正如我所说,如果我有正确的用户名/密码,它就可以工作。它会显示我的警报消息,但如果它不正确,它不会显示任何警报消息。

private static void performUserConnection(String username, String password) {
    DBConnectionAsync rpcService = (DBConnectionAsync) GWT.create(DBConnection.class);
    ServiceDefTarget target = (ServiceDefTarget) rpcService;
    String moduleRelativeURL = GWT.getModuleBaseURL() + "DBConnectionImpl";
    target.setServiceEntryPoint(moduleRelativeURL);

    rpcService.authenticateUser(username, password, new AsyncCallback<User>() {

        @Override
        public void onSuccess(User user) {
            Window.alert("TRALALA. Username: " + user.getUsername());
        }

        @Override
        public void onFailure(Throwable caught) {
            Window.alert("LALALALAL");
            // DialogBox dialogBox = createDialogBox();
            // dialogBox.setGlassEnabled(true);
            // dialogBox.setAnimationEnabled(true);
            // dialogBox.center();
            // dialogBox.show();
        }
    });
}

更新服务器部分。

public class DBConnectionImpl extends RemoteServiceServlet implements DBConnection {

private static final long serialVersionUID = 1L;

private String URL = new String("jdbc:mysql://localhost:3306");
private String user = "root";
private String pass = "andrei";
private String schema = "administrare_bloc";

public DBConnectionImpl() {
}

private Connection getConnection() throws Exception {
    Properties props = new Properties();
    props.setProperty("user", user);
    props.setProperty("password", pass);
    props.setProperty("zeroDateTimeBehavior", "convertToNull");
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection(URL + "/" + schema, props);

    return conn;
}

@Override
public User authenticateUser(String username, String password) throws Exception {

    User returnUser = null;
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
        conn = getConnection();

        try {
            String q = "select * from users where username=? and password=?";
            stmt = conn.prepareStatement(q);
            stmt.setString(1, username);
            stmt.setString(2, password);
            rs = stmt.executeQuery();

            while (rs.next()) {
                int id = rs.getInt("id");
                String user = rs.getString("username");
                String pass = rs.getString("password");
                String type = rs.getString("type");

                returnUser = new User(id, user, pass, type);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        rs.close();
        stmt.close();
        conn.close();
    }

    return returnUser;
}

}

提前致谢

【问题讨论】:

  • 你能展示一下服务器部分吗?如果密码错误,服务器会发生什么?
  • 忘记了。对不起。刚刚编辑。

标签: java gwt


【解决方案1】:

只有在服务器上抛出异常时才会调用 onFailure 方法。现在,如果没有找到用户,您只需返回一个空对象。

【讨论】:

  • 谢谢。我不知道。我有一个小问题,如果你不介意的话。我最近将密码字段更改为 PasswordTextBox(最初是 TextBox),我注意到 TextBox 的大小与 PasswordTextBox 的大小不同。我怎样才能让他们平等?
  • 这是您另一个问题的答案:stackoverflow.com/questions/11518591/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
相关资源
最近更新 更多