2

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
}

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

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

FacesContext context = FacesContext.getCurrentInstance();
SessionBean sessionBean = context.getApplication().evaluateExpressionGet(context,
    "#{sessionBean}", SessionBean.class);
Eduardo
  • 63
  • 1
  • 8

2 Answers2

4

This is a known issue:

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

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>
Eduardo
  • 63
  • 1
  • 8
0

I use GAE (Google App Engine) and need to set javax.faces.STATE_SAVING_METHOD to client. This problem can have workaround. After the action, just call refreshSession() with new value force the session object persist

protected void refreshSession(){
    saveSession(CeaConst.SESSION_ATTR_NAME_LAST_REFRESH_TIME, new java.util.Date());
}
songjing
  • 545
  • 4
  • 22