开发者

Display users email when logged in (JSF 2.0)

开发者 https://www.devze.com 2023-02-27 00:40 出处:网络
My web app has a template that is used in all pages. I want to create a little label in that template (so it\'s visible everywhere) to display the email of the user if it\'s currently logged in.

My web app has a template that is used in all pages. I want to create a little label in that template (so it's visible everywhere) to display the email of the user if it's currently logged in.

I think the best way is to directly see if it exist on the session, so this is how I created the label:

<h:outputLabel id="usernameLabel"
               value="#{FacesContext.ge开发者_开发技巧tCurrentInstance().getExternalContext()
                      .getSessionMap().get("userRole")}" />

The code is not syntactically correct. How can I write it in the appropriate manner to achieve my goal? Do you think this approach is correct?


The better approach would be to store userBean in session containing the user's information.

and then

#{userBean.email}

Or you can directly attribute from session using jsp el

${userRole}

I would prefer first approach

Update

Upon successful login, set the frequently required in a ManagedBean for example

@ManagedBean
@SessionScoped
public class CurrentUserInfo{
  private String userName;
  private String firstName;
  private String lastName;
  //and some other like DOB and etc..
  //+accessor methods 
}

This would be alive and accessible through the session

0

精彩评论

暂无评论...
验证码 换一张
取 消