How to know if I am in a postback?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 11:17:27

问题


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


回答1:


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



回答2:


Indeed, before jsf1.2, isPostBack was obtained through the requestScope of the current instance of FaceContext.

Since JSF1.2, The ResponseStateManager (helper class to StateManager that knows the specific rendering technology being used to generate the response, a singleton abstract class, vended by the RenderKit.)

During the restore view phase of the life cycle, ViewHandler retrieves the ResponseStateManager object in order to test if the request is a postback or an initial request.

If a request is a postback, therestoreView method of ViewHandler is called. This method uses theResponseStateManager object to re-build the component tree and restore state. After the tree is built and state is restored, theViewHandler instance is not needed until the render response phase occurs again.

That article mentionned above (Creating and Using a Custom Render Kit) illustrates how to implement/get an ResponseStateManager, through a RenderKit (defined by the tag handler implementing the tag that renders the component).
May be this is enough for you to get your own ResponseStateManager in your context ?




回答3:


For JSF1.2

public static boolean isPostback(){
    FacesContext context = FacesContext.getCurrentInstance();
    return context != null && context.getExternalContext().getRequestParameterMap().containsKey(ResponseStateManager.VIEW_STATE_PARAM);
}


来源:https://stackoverflow.com/questions/427272/how-to-know-if-i-am-in-a-postback

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!