2011-07-15 122 views
2

對於viewBean的第一個「nonpostback」請求,sessionBean中的someValue屬性爲null。 現在,在回發請求中,我將用戶輸入設置爲someValue。問題是someValue在任何「nonpostback」請求中始終爲空。 這裏是我的代碼:會話Bean正在丟失?

@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 任何建議表示讚賞。謝謝。

UPDATE: 在這兩種方法(init和動作)使用evaluateExpressionGet正常工作:

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

回答

0

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