0

I have a bean class which looks like this

@ManagedBean(name = "usingBean")
@SessionScoped
public class UserInfo implements Serializable {

    private static final long serialVersionUID = 2668727340500045081L;

    String loginId;

}

I set this bean attributes in a filter class.

I am trying to get this attribute in another bean class

@ManagedProperty(value = "#{usingBean}")
private UserInfo user;

public UserInfo getUser() {
    return user;
}

public void setUser(UserInfo user) {
    this.user = user;
}
UserInfo neededBean = (UserInfo) context.getApplication()
                .createValueBinding("#{usingBean}").getValue(context);
                return neededBean.getLoginId();

It says null when I try to print it, but it does get inserted into the DB. It does not change when a different user logs in.

Brian
  • 3,850
  • 3
  • 21
  • 37
CSD
  • 11
  • 1

1 Answers1

0

Try with simple way, in your filter class set your attribute to session

       FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context
                .getExternalContext().getRequest();
        HttpSession httpSession = request.getSession(false);
        httpSession.setAttribute("loginId", loginId);

In the other class, you can get the "loginId" from session ...

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context
                .getExternalContext().getRequest();
        HttpSession httpSession = request.getSession(false);
        String loginId= (String) httpSession.getAttribute("loginId");
9ine
  • 879
  • 2
  • 8
  • 18