我是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>
这是Login 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();
}
}
}
这是User类:
@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。为什么会这样?我该如何解决?
答案 0 :(得分:1)
设置者正常工作,否则您的NullPointerException
方法会获得Login#checkDB
。问题是看起来您的Login
托管bean标记为@RequestScoped
或@ViewScoped
(版本后@RequestScoped
)并且您正在重定向到 main.xhtml 而不是转发,注意到这一行:
FacesContext.getCurrentInstance().getExternalContext().redirect("main.xhtml");
请注意,您在User
方法中手动创建Login#checkDB
类的实例,因此它将表现为POJO,并且在方法的末尾将其标记为垃圾回收(这是基本的Java)。另请注意,执行重定向时,所有请求属性都将丢失。
要解决您的问题,您应该在会话中保存用户数据,让CDI容器管理bean而不是手动执行。为此,您需要注入@SessionScoped
托管bean来处理用户数据(密码除外,以避免安全问题)。这里解释了这一点:Get access to session scoped CDI bean from request scoped CDI Bean(请注意,此Q / A提供了一个关于登录的示例)。
重要提示:不要将JSF与CDI bean混合使用,您应该使用一个或另一个,但不要同时使用。将您的User
bean从@ManagedBean
更改为@Named
以使用CDI电源。