Попытка установить и получить атрибут из одного класса в другой через 3-й класс в JAVA

У меня есть класс bean, который выглядит так

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

    private static final long serialVersionUID = 2668727340500045081L;

    String loginId;

}

Я установил атрибуты этого компонента в классе фильтра.

Я пытаюсь получить этот атрибут в другом классе компонентов

@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();

Когда я пытаюсь его распечатать, он говорит null, но он вставляется в БД. Он не меняется при входе в систему другого пользователя.


person CSD    schedule 30.06.2016    source источник


Ответы (1)


Попробуйте простым способом, в своем классе фильтра установите для атрибута сеанс

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

В другом классе вы можете получить "loginId" из сеанса...

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context
                .getExternalContext().getRequest();
        HttpSession httpSession = request.getSession(false);
        String loginId= (String) httpSession.getAttribute("loginId");
person 9ine    schedule 01.07.2016
comment
Вы отлаживаете свой код? Когда значение вашего сеанса изменено на null! - person 9ine; 04.07.2016
comment
Я понял.. Большое спасибо! - person CSD; 07.07.2016