Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
861 views
in Technique[技术] by (71.8m points)

jsf - How to know if I am in a postback?

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

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...