Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've read in JSF docs that ResponseStateManager has a isPostBack() method. How (and where) can I have an instance of ResponseStateManager?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
872 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...