0
votes

I have a weird problem, I am using JSF 2.1.6 and Primefaces 3.4.

The problem is that my @PostConstruct in my @ViewScoped bean is fired again after an onSelectNavigate, which causes NPE's because stuff is not set anymore in the Flash Scope.

I have the following init() method

@PostConstruct public void init() {

    log.debug("initing DashBoard");

    epsDashboardVos = new ArrayList<>();

    for (Eps eps : epsService.getEpss()) {
        /// do some stufff

    }

}

and in the same bean the following navigate method

public void onSelectedEpsNavigate(EpsDashboardVo selectedEps) {

    log.debug("Selecting eps and moving to detail screen : "
            + selectedEps.getEps().getName());

    FacesContext.getCurrentInstance().getExternalContext().getFlash()
            .put("selectedEps", selectedEps.getEps());

    // adjust header
    menuController.setCurrentPage("View EPS Status - "
            + selectedEps.getEps().getName());

    ConfigurableNavigationHandler configurableNavigationHandler = (ConfigurableNavigationHandler) FacesContext
            .getCurrentInstance().getApplication().getNavigationHandler();

    configurableNavigationHandler
            .performNavigation("epsdashboard-detail-view?faces-redirect=true");
}

What I then see in the log is that the navigate method is called but then the init() method in the same bean is called again.

  17 Oct 2012 11:54:07,244 DEBUG com.xxxx.eps.subscription.controller.EpsDashboardViewController : initing DashBoard
  17 Oct 2012 11:54:09,550 DEBUG com.xxxx.eps.subscription.controller.EpsDashboardViewController : Selecting eps and moving to detail screen : M0951-EPS2X-DEV-Commercial
  17 Oct 2012 11:54:09,553 DEBUG com.xxxx.eps.subscription.controller.EpsDashboardViewController : initing DashBoard
  17 Oct 2012 11:54:09,639 DEBUG com.xxxx.eps.subscription.controller.EpsDashboardDetailViewController : initing DashBoard
1

1 Answers

0
votes

You're navigating to a different view. That's the end of the current view scope. If the same backing bean class is also referenced as a view scoped bean in the other view for some reason, then a new instance will be created for the new view scope.

The concrete functional requirement is not clear, so it's not possible to propose the right approach for whatever you're trying to achieve. Perhaps the easiest way to "fix" this problem would be adding a nullcheck in the @PostConstruct.