2017-02-27 64 views
0

我有一個JSF項目,我正在嘗試做一個登錄頁面,在我的項目中我有一個託管的bean,它擁有用戶名和密碼的validate方法,並且我有一個包含setter的bean類和獲取者,其中有用戶信息填充數據庫如(用戶名,密碼,isActive,全名),我的問題是如何在我的xhtml頁面中調用JSF el表達式中的用戶信息,如果他們不在託管bean?JSF bean和對象

這是我的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="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 getUserId() { 
     return userId; 
    } 

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

    public String getPassword() { 
     return password; 
    } 

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

這裏是我的MBLogin:

@ManagedBean 
    @SessionScoped 
    public class MBLogin { 

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

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

你能告訴我們你的代碼嗎? –

+0

沒有得到託管bean? –

+0

@khalilM我想訪問託管bean,並且只有在託管的bean中複製了我的實例變量和setter以及getter時才能訪問它們,但是這樣做沒有意義嗎?我很抱歉,但我是JSF的新手 – Khaled

回答

1

添加一個getter用戶:

@ManagedBean 
    @SessionScoped 
    public class MBLogin { 

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

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

然後在你的XHTML,你可以這樣稱呼:
#{user.id}#{user.firstName}

+0

感謝主席,工作得很好:D – Khaled

+0

爲我的getters工作,但現在我的setter不工作有什麼辦法解決它們? – Khaled

+0

只需將一個setter添加到managedbean。 –