0

Anybody know what could have caused JSF to instantiate a new viewScoped bean even though the viewstate id is the same.

In my viewScoped bean constructor, I put codes below to print the value of javax.faces.ViewState:

// construtor
public MyViewScopeBeanJsfBean() {
    System.out.println("===== In MyViewScopeBeanJsfBean Constructor Begin =====");
    printViewState();
    System.out.println("===== In MyViewScopeBeanJsfBean Constructor End =====");
}

private void printViewState() {
    String[] viewStates = getHttpServletRequest().getParameterValues("javax.faces.ViewState");
    if (viewStates != null) {   
        for (String viewState :viewStates) {
            System.out.println("javax.faces.ViewState="+viewState);
        }
    }
    else {
        System.out.println("javax.faces.ViewState=null");           
    }       
}

Below is the result I'm getting:

JSF created the viewscoped bean for the 1st time:
===== In MyViewScopeBeanJsfBean Constructor Begin =====
javax.faces.ViewState=null
===== In MyViewScopeBeanJsfBean Constructor End =====


I stayed on the same page and clicked on a ajax link to inlcude a page into the current page.
===== In MyViewScopeBeanJsfBean Constructor Begin =====
javax.faces.ViewState=y25eUekk0MqNOV7cxixhe+YOI31giRnbuL1ad9X158PdRuir
===== In MyViewScopeBeanJsfBean Constructor End =====

I then click on a link In the newly included page to upload a file:
===== In MyViewScopeBeanJsfBean Constructor Begin =====
javax.faces.ViewState=y25eUekk0MqNOV7cxixhe+YOI31giRnbuL1ad9X158PdRuir
===== In MyViewScopeBeanJsfBean Constructor End =====

On each of those clicks, I stay on the same page, and you can see that the viewstate Id are the same. But why did JSF call the constructor to create a new instance of the viewscoped bean?

Note: I do have the following in my main template to correct the viewscoped bean issue related to ajaxly include a page:

<h:outputScript library="omnifaces" name="fixviewstate.js" target="head" />
HockChai Lim
  • 1,675
  • 2
  • 20
  • 30

0 Answers0