JSF EL表达式和Java bean

时间:2017-02-27 13:23:02

标签: jsf javabeans managed-bean

我有一个JSF项目,我正在尝试创建一个登录页面,我已经设法从数据库中获取用户名和密码并验证它们,我的项目有一个Java bean,托管bean和DAO类,当用户成功登录后,我想打印Hello先生 < h:outputLabel value="#{mBLogin.user.firstName}" /> Hello先生正在打印,但名称不是,虽然在测试我的DAO课程时我将名称打印到控制台没有任何问题!有人可以建议我做错了吗?

我的托管bean类:

    @ManagedBean
    @SessionScoped
    public class MBLogin {

        User user = new User();
        LoginDAO loginDao = new LoginDAO();

        public String validteStudent() {
            boolean valid = loginDao.validateStudent(user.getUserId(), user.getPassword());
            if (valid) {
                user.getFirstName();
                HttpSession session = SessionUtils.getSession();
                session.setAttribute("username", user);
                return "admin";
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                        "Incorrect Username and Passowrd", "Please enter correct username and Password"));
                return "login";
            }
        }

        public void setUser(User user) {
            this.user = user;
        }

        public User getUser() {
            return user;
        }
    }

我的Java Bean类:

    @Table(name = "students_info")
    public class User {

        @Column(name = "std_record_id")
        private int id;

        @Column(name = "std_id")
        private String userId;

        @Column(name = "first_name")
        private String firstName;

        @Column(name = "last_name")
        private String lastName;

        @Column(name = "web_password")
        private String password;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

    }

我的DAO课程:

    public class LoginDAO {

        static JKPlainDataAccess dataAccess = JKDataSourceFactory.getPlainDataAccess();
        User user;

        public boolean validateStudent(String userName, String password) {

            user = dataAccess.executeQueryAsSingleObject(User.class, "id,userId,firstName,lastName,password",
                    "SELECT std_record_id, std_id, first_name, family_name, web_password From  students_info WHERE std_id=? and web_password=?",
                    userName, password);
            JK.print("getAllEmployeeRecords() : ", user);
            if(user != null) {
                System.out.println(user.getFirstName());
                System.out.println(user.getLastName());
                return true;
            }
            return false;
        }

        public static void main(String[] args) {
            LoginDAO a = new LoginDAO();
            a.validateStudent("200663042001", "1234");
        }

    }

登录页面后的我的xhtml页面:

<ui:composition template="/WEB-INF/layouts/default.xhtml">
     <ui:define name="content">
     WELCOME Mr. <h:outputLabel value="#{mBLogin.user.firstName}" />
    AND <h:outputLabel value="#{mBLogin.user.lastName}" />
    </ui:define>
 </ui:composition>

1 个答案:

答案 0 :(得分:1)

验证时,您似乎将用户作为会话属性而不将其分配给Managed Bean字段:

session.setAttribute("username", user);

因此,要么将其分配给实例用户变量,要么只使用:

<ui:composition template="/WEB-INF/layouts/default.xhtml">
     <ui:define name="content">
     WELCOME Mr. <h:outputLabel value="#{username.firstName}" />
    AND <h:outputLabel value="#{username.lastName}" />
    </ui:define>
 </ui:composition>

<强>更新

我建议您将服务方法更改为:

public User validateStudent(..)

您实际返回查询的用户,而不是在DAO中设置它。

因此您可以将ManagedBean方法更改为:

public String validteStudent() {
     User validatedUser = loginDao.validateStudent(user.getUserId(), user.getPassword());
     if (validatedUser != null) {
           this.user = validatedUser;
           HttpSession session = SessionUtils.getSession();
           session.setAttribute("username", user);
      ....