0

我是 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。为什么会这样?我该如何解决?

4

1 回答 1

1

二传手工作正常,否则你会NullPointerException在你的Login#checkDB方法中得到一个。问题是看起来您的Login托管 bean 被标记为@RequestScopedor @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 电源。

于 2013-07-04T17:40:26.350 回答