Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if I am in a postback?

Tags:

jsf

postback

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

like image 818
Pier Luigi Avatar asked Jan 09 '09 06:01

Pier Luigi


People also ask

How do we identify that the page is postback?

Which property is used to identify the Page is Post Back in ASP.NET? Page. IsPostBack property is use to check wheather page is post back.It return bool value.

What happens during postback?

PostBack is the name given to the process of submitting an ASP.NET page to the server for processing. PostBack is done if certain credentials of the page are to be checked against some sources (such as verification of username and password using database).

How do you know which control is caused by postback?

All controls accept Button and ImageButton use JavaScript for causing a postback. To enable postback on these controls one has to set AutoPostBack property to true. When you set this property to true, __doPostBack function is called on event which causes a postback.

What is the meaning of postback?

In web development, a postback is an HTTP POST to the same page that the form is on. In other words, the contents of the form are POSTed back to the same URL as the form. Postbacks are commonly seen in edit forms, where the user introduces information in a form and hits "save" or "submit", causing a postback.


3 Answers

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();
}
like image 146
BalusC Avatar answered Sep 21 '22 15:09

BalusC


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 ?

like image 34
VonC Avatar answered Sep 20 '22 15:09

VonC


For JSF1.2

public static boolean isPostback(){
    FacesContext context = FacesContext.getCurrentInstance();
    return context != null && context.getExternalContext().getRequestParameterMap().containsKey(ResponseStateManager.VIEW_STATE_PARAM);
}
like image 23
bravocharlie Avatar answered Sep 19 '22 15:09

bravocharlie