You've stored the logged-in user as a session attribute with the name "user".
So it's in EL available as follows:
#{user}
You can just use the empty
keyword in EL to check if it's present or not (i.e. if it's logged-in or not) and you can use JSF component's rendered
attribute to instruct whether to generate HTML output or not.
<h:panelGroup rendered="#{not empty user}">
<p>Welcome #{user}, you have been logged in!</p>
</h:panelGroup>
In your specific case of hiding a table column, just use it as follows:
<p:column rendered="#{not empty user}">
...
</p:column>
A nicer way to store a session attribute is using ExternalContext#getSessionMap()
. This way your code is free of smelly javax.servlet.*
imports.
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", username);
Unrelated to the concrete problem, why the following check?
if (username.equals(getUsername_db()) && password.equals(getPassword_db())) {
Why don't you just do a SELECT ... FROM user WHERE username=? AND password=?
in SQL? Or don't you trust the DB that it returns the right row?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…