ManagedProperty of SessionScope inside a ViewScoped Bean - Transient?

给你一囗甜甜゛ 提交于 2019-12-04 07:22:00

This won't work. If the view scoped bean is serialized, all transient fields are skipped. JSF doesn't reinject managed properties after deserialization, so you end up with a view scoped bean without a session scoped bean property which will only cause NPEs.

In this particular construct, your best bet is to introduce lazy loading in the getter and obtain the session bean by the getter instead of by direct field access.

private transient SessionBeany sessionBeany;

public SessionBeany getSessionBeany() { // Method can be private.
    if (sessionBeany == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        sessionBeany = context.getApplication().evaluateExpressionGet(context, "#{sessionBeany}", SessionBeany.class);
    }

    return sessionBeany;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!