会话Bean丢失了吗? [英] Session Bean being lost?

查看:130
本文介绍了会话Bean丢失了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The first "nonpostback" request to viewBean, someValue property in sessionBean is null. Now, in a postback request, I am setting a user input to someValue. The problem is that someValue is always null in any "nonpostback" request. Here is my code:

@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 任何建议表示赞赏.谢谢.

I feel I am doing something wrong. I am using Mojarra 2.1.2 Any advice is appreciated. Thank you.

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

UPDATE: Using evaluateExpressionGet on both methods (init and action) works fine:

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

推荐答案

这是一个已知问题:

ViewScoped bean中的SessionScoped bean被解析为不同的bean,具体取决于所使用的表达式

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

I just changed the state saving method in my web.xml:

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

这篇关于会话Bean丢失了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆