JSF 2视图范围状态保存未正确保存注入的属性和ejb

时间:2013-08-15 21:42:42

标签: jsf-2 ejb websphere view-scope managed-property

我目前正在使用带有JSF 2.0(MyFaces)的Websphere 8.5。由于@ViewScoped bean的状态保存(服务器和客户端模式),我一直面临一些问题。我有以下bean

@ManagedBean
@ViewScoped
public class BrokenBean implements Serializable {

    //Problem: not synchronized with real SessionBean object
    @ManagedProperty(value="#{sessionBean})
    private SessionBean session;

    //Problem: this gives NotSerializableException to some server generated class.
    @EJB
    private MyEJB myejb;

    //Getter and Setter for above properties
}

@ManagedBean
@SessionScoped
public class SessionBean implements serializable{
    private Integer i;
    //Getter and Setter
}

//MyEJB has a method to get Counter from database

我的测试页包含

<h:outputText value="#{brokenBean.session.i}"></h:outputText>
<h:outputText value="#{brokenBean.myejb.getCounter()}"></h:outputText>

状态保存时两者都会失败。

对于SessionBean,当我使用BrokenBean输入i = 1的页面,并在另一页面中设置i = 2时,使用BrokenBean的页面仍将显示i = 1.

对于MyEJB,这只会给一些名为“* Local * MyEJB”的服务器生成的类提供一个NotSerializableException,其中*代表一些我不记得的字符。

我使用了一种可行的解决方法,但它就像“假设是透明的”JSF一样不必要的复杂化。有没有其他解决方法看起来更干净?

我最终得到了这堂课:

@ManagedBean
@ViewScoped
public class FixedBrokenBean implements Serializable {

    private transient SessionBean session;

    private transient MyEJB myejb;

    //I must use getters below when I access these two properties within this class
    //so that getters will perform a lookup if they are null at the time I need them.

    public MyEJB getMyejb() {
        if (myejb == null) {
            try {
                InitialContext ic = new InitialContext();
                myejb = (MyEJB) ic.lookup("java:global/testear/testejb/MyEJB !test.MyEJB");
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
        return myejb;
    }

    public SessionBean getSession() {
        if (session == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            session = (SessionBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionBean.class);
        }
        return session;
    }
}

0 个答案:

没有答案