会话Bean丢失了?

时间:2011-07-15 18:04:58

标签: jsf-2

对viewBean的第一个“nonpostback”请求,sessionBean中的someValue属性为null。 现在,在回发请求中,我将用户输入设置为someValue。问题是someValue在任何“nonpostback”请求中始终为null。 这是我的代码:

@ManagedBean
@ViewScoped
public class ViewBean implements Serializable {

    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;

    private String inputText;

    @PostConstruct
    public void init() {
        if (sessionBean.getSomeValue() != null) // ALWAYS NULL
            doSomething(sessionBean.getSomeValue());
    }

    private void doSomething(String s) {}

    public void action(final ActionEvent ae) {
        sessionBean.setSomeValue(getInputText());
        doSomething(getInputText());
    }

    GETTERS/SETTERS
}

@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {

    private String someValue;

    GETTER/SETTER
}

我觉得我做错了什么。我正在使用Mojarra 2.1.2 任何建议表示赞赏。谢谢。

更新: 在两个方法(init和action)上使用evaluateExpressionGet工作正常:

FacesContext context = FacesContext.getCurrentInstance();
SessionBean sessionBean = context.getApplication().evaluateExpressionGet(context,
    "#{sessionBean}", SessionBean.class);

2 个答案:

答案 0 :(得分:4)

这是一个众所周知的问题:

SessionScoped bean inside a ViewScoped bean is resolved as different bean depending on the expression used

我刚刚在web.xml中更改了状态保存方法:

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

答案 1 :(得分:0)

我使用GAE(Google App Engine)并需要将javax.faces.STATE_SAVING_METHOD设置为客户端。此问题可以有解决方法。在操作之后,只需使用新值强制调用refreshSession(),会话对象持久化

protected void refreshSession(){
    saveSession(CeaConst.SESSION_ATTR_NAME_LAST_REFRESH_TIME, new java.util.Date());
}