0

有人可以帮我解决这个错误吗?它在 UserDao.login 和 LoginBean.loginproject 处显示 NullPointerException。我是 JSF 2 中过滤器的新手。方法 session.getAttribute("attribute") 中使用了哪个属性?

非常感谢你

问候

用户道.java

public class UserDAO {     

    ...

    public static Connection getInstance(){
        if(connection == null)
            new UserDAO();
        return connection;
    }

     public static boolean login(String user, String password) throws SQLException {
            connection = getInstance();
            String Query1 = "SELECT nama, password " + "FROM user " + "WHERE nama = ? AND password = ?";
            PreparedStatement statement1 = (PreparedStatement) connection.prepareStatement(Query1);
            statement1.setString(1, user);
            statement1.setString(2, password);

            ResultSet rs = statement1.executeQuery();
            if (rs.next()) // found
            {
                System.out.println(rs.getString("nama"));
                return true;
            }
            else {
                return false;
            }
    }
}

登录Bean.java

public class LoginBean implements Serializable {

 ...
//getters setters

    public String loginProject() throws SQLException {
        boolean result = UserDAO.login(uname, password);
        if (result) {
            // get Http Session and store username
            HttpSession session = Util.getSession();
            session.setAttribute("username", uname);

            return "home";
        } else {

            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Invalid Login!",
                    "Please Try Again!"));
            return "login";
        }
    }

    public String logout() {
      HttpSession session = Util.getSession();
      session.invalidate();
      return "login";
   }
}
4

1 回答 1

1

In JSF you don't need to do anything with regard to the Session object, unless it is explicitly needed. By putting managed beans in session scope you are lifting off the necessity of dealing with the 'raw' session object as it was in the times of Servlet+JSP combo: Faces Servlet will manage the job for you as soon as you refer to session-scoped beans in your view.

So, when you use somthing with a session.setAttibute flavour means that you do not want to use all the benefits JSF 2.0 provides for you out-of-the-box.

Stating this, you shall have a session-scoped bean that is holding the currently logged-in user. The login action can be moved to a request scoped bean associated with a simple view containing login form (login input field / password input field / submit login button). These two beans may look like

@ManagedBean
@RequestScoped
public class LoginUser {

    @EJB
    private UserService userService;

    @ManagedProperty(value="#{userManager}")
    private UserManager userManager;

    private String login;
    private String password;

    public LoginUser() {
    }

    public String login() {
        //checking for nulls and empty strings
        User user = userService.find(login, password);
        if(user == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Wrong username or password"));
            userManager.setCurrentUser(null);
            return null;
        }
        userManager.setCurrentUser(user);
        return "mypersonalspace.xhtml";
    }

}

and

@ManagedBean
@SessionScoped
public class UserManager {

    private User currentUser;

    public UserManager() {
    }

}

In this setup the session will contain all the data you gave it, in our case a User object. You can always refer to it in your views by #{userManager.currentUser.username}, for example.

As a side note, you MUST always close JDBC-related elements (ResultSets / Statements and in the first place Connections) to prevent leakage of resources. And also, take time and read literature on EJBs to use DAOs/Services appropriately.

于 2013-02-14T15:21:31.173 回答