我是 JSF 的新手,并且有一个登录页面。这里是:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2" border="1">
<h:outputText value="User:"></h:outputText>
<h:inputText id="username" value="#{login.username}"></h:inputText>
<h:outputText value="Password"></h:outputText>
<h:inputSecret id="password" value="#{login.password}"></h:inputSecret>
<h:commandButton value="Login" action="#{login.checkDB}"></h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
这是登录 jsf 页面
@Named(value = "login")
@Dependent
@RequestScoped
public class Login{
private String username = "", password = "";
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setUsername(String Username) {
username = Username;
}
public void setPassword(String Password) {
password = Password;
}
public void checkDB() {
try {
con=DB.connect();
PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
"SELECT * FROM Users where username = ? AND password = ?");
checkDB.setString(1, username);
checkDB.setString(2, password);
ResultSet rs = null;
rs = (ResultSet) checkDB.executeQuery();
if (rs.next()) {
User.customer = new Customer(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7));
rs.close();
DB.disconnect(con);
FacesContext.getCurrentInstance().getExternalContext().redirect("main.xhtml");
} else {
rs.close();
DB.disconnect(con);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是用户类:
@ManagedBean(name = "user")
@SessionScoped
public class User implements Serializable {
public static Customer customer;
private Connection con=null;
public void setCustomer(Customer customer) {
User.customer = customer;
}
public Customer getCustomer() {
return customer;
}
public User() {
}
public void editInfo() {
String name = customer.getName(), surname = customer.getSurname(), password = customer.getPassword();
String phone = customer.getPhone(), address = customer.getAddress(), email = customer.getEmail();
try {
con=DBConnect.connect();
int result = -1;
PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
"UPDATE users set password=?,name=?,surname=?,phone=?,address=?,"
+ "email=? where username=?");
checkDB.setString(7, customer.getUsername());
checkDB.setString(1, password);
checkDB.setString(2, name);
checkDB.setString(3, surname);
checkDB.setString(4, phone);
checkDB.setString(5, address);
checkDB.setString(6, email);
result = checkDB.executeUpdate();
con.close();
FacesContext.getCurrentInstance().getExternalContext().redirect("editsuccesfull.xhtml");
} catch (Exception e) {
e.printStackTrace();
}
}
问题是,在 setter 函数设置用户名和密码后,当函数从托管 bean 返回时,它们突然变为 null。为什么会这样?我该如何解决?