How to know if I am in a postback?
Depends on JSF version.
In JSF 1.0/1.1, there's no ResponseStateManager#isPostback()
method available. check if javax.faces.ViewState
parameter is present in the request parameter map as available by ExternalContext#getRequestParameterMap()
.
public static boolean isPostback() {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
return externalContext.getRequestParameterMap().contains("javax.faces.ViewState");
}
In JSF 1.2, indeed use ResponseStateManager#isPostback()
which in turn actually checks the presence of javax.faces.ViewState
parameter in the request parameter map.
public static boolean isPostback() {
FacesContext context = FacesContext.getCurrentInstance();
return context.getRenderKit().getResponseStateManager().isPostback(context);
}
In JSF 2.0, instead use FacesContext#isPostback()
, which under the covers actually delegates to ResponseStateManager#isPostback()
.
public static boolean isPostback() {
return FacesContext.getCurrentInstance().isPostback();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…