2016-08-26 95 views
0

我想在登錄後顯示登錄的用戶全名,但需要一些direction.I已經驗證用戶對數據庫,但它只能從login.How返回電子郵件有關登錄用戶的信息。我有三個類LoginDAO,Session和managedbean,loginDAO包含從數據庫中獲取所需信息的查詢。在java中登錄後返回全名

public class Users { 
    private int userid; 
    private String fname; 
    private String lname; 
    private String email; 
    private String password; 
    private String profileimg; 
    Profile uprofile = new Profile(); 

public Profile getUprofile() { 
    return uprofile; 
} 

public void setUprofile(Profile uprofile) { 
    this.uprofile = uprofile; 
} 

public String getProfileimg() { 
    return profileimg; 
} 

public void setProfileimg(String profileimg) { 
    this.profileimg = profileimg; 
} 

public int getUserid() { 
    return userid; 
} 

public void setUserid(int userid) { 
    this.userid = userid; 
} 

public String getFname() { 
    return fname; 
} 

public void setFname(String fname) { 
    this.fname = fname; 
} 

public String getLname() { 
    return lname; 
} 

public void setLname(String lname) { 
    this.lname = lname; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getPassword() { 
    return password; 
} 

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

}

public class LoginDAO {   
    public static Users validate (String user,String password) throws SQLException{ 
     Connection con = null; 
    PreparedStatement ps = null; 
    Users validUser = null; 

     try{ 
      con = DataConnect.getConnection(); 
      ps = con.prepareStatement("Select fname,lname,email, password from users where email = ? and password = MD5(?)"); 
      ps.setString(1, user); 
      ps.setString(2, password); 

      ResultSet rs = ps.executeQuery(); 

      if(rs.next()) { 
      validUser = new Users();    
       validUser.setFname(rs.getString("fname")); 
       validUser.setLname(rs.getString("lname")); 
       return true; 
      return validUser; 
        } 
    }catch (SQLException ex){ 
    System.out.println("Login error -->" + 
      ex.getMessage()); 
    return null; 
}finally{ 
    DataConnect.close(con); 
} 
return validUser; 

}}

這是管理bean @ManagedBean(name = 「loginbean」) @SessionScoped 公共類LoginBean實現Serializable { 私人字符串FNAME; private String pwd; private String msg; 私人字符串用戶;

public String getFname() { 
     return fname; 
    } 

    public void setFname(String fname) { 
     this.fname = fname; 
    } 

    public String getPwd() { 
     return pwd; 
    } 

    public void setPwd(String pwd) { 
     this.pwd = pwd; 
    } 

    public String getMsg() { 
     return msg; 
    } 

    public void setMsg(String msg) { 
     this.msg = msg; 
    } 

    public String getUser() { 
     return user; 
    } 

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

此方法調用loginDAO並將其與用戶輸入相匹配並啓動會話。

public String validateUsernamePassword() throws SQLException { 
Users validUser = LoginDAO.validate(user, pwd); 
    if (validUser != null) { 
     HttpSession session = SessionUtils.getSession(); 
     session.setAttribute("username", validUser.getEmail()); 
    session.setAttribute("firstname", validUser.getFname()); 
     return "feed"; 
    } else { 

     return "login"; 
    } 
} 
//logout event, invalidate session 
public String logout() { 
    HttpSession session = SessionUtils.getSession(); 
    session.invalidate(); 
    return "login"; 
} 

    <h:form> 
      <p>Welcome <h:outputText id="username" value="#  
     {loginbean.user}"/></p> 
      <h:outputText id="firstname" value="#{loginbean.fname}"/> 
    <h:commandLink action="#{loginbean.logout}" value="Logout"> 
    </h:commandLink> 


</h:form> 
+1

請修復您的代碼格式化。 –

+0

和你的程序有什麼問題? – whyn0t

回答

1

您梅索德LoginDAO.validate(user, pwd);返回一個布爾值(true或false),而不是用戶對象。

這裏:

if(rs.next()){ 

    rs.getString("fname"); 
    rs.getString("lname"); 
    return true; 
} 

你得到「FNAME」「L-NAME」 從結果屬性,但你不救他們的任何地方!

你可以這樣改變這個方法:

public static UserObject validate (String user,String password) throws SQLException{ 
    Connection con = null; 
    PreparedStatement ps = null; 
    UserObject validUser = null; 
    try{ 
     con = DataConnect.getConnection(); 
     ps = con.prepareStatement("Select fname,lname,email, password from users where email = ? and password = MD5(?)"); 
     ps.setString(1, user); 
     ps.setString(2, password); 

     ResultSet rs = ps.executeQuery(); 

        if(rs.next()){ 
        validUser = new UserObject(); 
        validUser.setFname(rs.getString("fname")); 
        validUser.setLname(rs.getString("lname")); 
        validUser.setEmail(rs.getString("email")); 
        // ...etc. 
         return validUser; 
        } 
    }catch (SQLException ex){ 
     System.out.println("Login error -->" + 
       ex.getMessage()); 
     return null; 
    }finally{ 
     DataConnect.close(con); 
    } 
    return validUser; 
} 

則檢查其有效的登錄是這樣的:

public String validateUsernamePassword() throws SQLException { 
    UserObject validUser = LoginDAO.validate(user, pwd); 
    if (validUser != null) { 
     HttpSession session = SessionUtils.getSession();   
     session.setAttribute("username", validUser.getUsername()); 
     session.setAttribute("firstname",validUser.getFname()); 
     return "feed"; 
    } else { 
     return "login"; 
    } 
} 
+0

嗨Rami感謝您的幫助,它非常感謝登錄工作正常,但它不顯示用戶的名字 – user2491007

+0

請再次閱讀我的答案!您的validate方法只返回true或false,您不會將提取的數據保存到任何對象/變量,並且您的validateUsernamePassword中,您將獲得「fname」值?試着按照我的例子重寫它以適應你的需求/實現。它應該然後工作。或者你更新你的問題並提供完整的豆類/對象代碼等等。 –