gpt4 book ai didi

java - 无法捕获 onFailure() GWT AsyncCallback

转载 作者:行者123 更新时间:2023-11-29 02:58:35 28 4
gpt4 key购买 nike

我正在创建一个需要用户身份验证的应用程序。我在尝试登录时遇到一个问题。当我输入正确的用户名和密码时,将调用 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;
}

}

提前致谢

最佳答案

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

关于java - 无法捕获 onFailure() GWT AsyncCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com