【问题标题】:Session Bean being lost?会话 Bean 丢失?
【发布时间】:2011-07-15 18:04:58
【问题描述】:

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

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

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    这是一个已知问题:

    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>
    

    【讨论】:

    • 如果我们想将javax.faces.STATE_SAVING_METHOD 保留为client,任何解决方案?
    【解决方案2】:

    我使用 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());
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 2017-11-26
      • 2015-04-18
      • 1970-01-01
      • 2013-08-10
      • 2012-05-09
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多