【问题标题】:JSF @PostConstruct fires again after a ConfigurableNavigationHandler executeJSF @PostConstruct 在 ConfigurableNavigationHandler 执行后再次触发
【发布时间】:2012-10-07 15:09:51
【问题描述】:

我有一个奇怪的问题,我使用的是 JSF 2.1.6 和 Primefaces 3.4。

问题是我的@ViewScoped bean 中的@PostConstruct 在 onSelectNavigate 之后再次被触发,这会导致 NPE,因为 Flash Scope 中不再设置东西。

我有以下init()方法

@PostConstruct 公共无效初始化(){

    log.debug("initing DashBoard");

    epsDashboardVos = new ArrayList<>();

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

    }

}

在同一个 bean 中使用以下导航方法

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

然后我在日志中看到的是,调用了 navigate 方法,但随后再次调用了同一个 bean 中的 init() 方法。

  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

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    您正在导航到不同的视图。这是当前视图范围的结束。如果由于某种原因,相同的支持 bean 类在另一个视图中也被引用为视图范围 bean,那么将为新的视图范围创建一个新实例。

    具体的功能要求不明确,因此不可能针对您想要实现的任何目标提出正确的方法。也许“解决”这个问题的最简单方法是在 @PostConstruct 中添加一个 nullcheck。

    【讨论】:

    • 每个页面都有自己的 ViewScoped 控制器 bean。
    • 在 postconstruct 方法上放置一个断点,并在“不必要的”postconstruction 期间调查调用堆栈,以了解是谁发起了它以及为什么发起它。如果您无法破译,请将其包含在您的问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2014-04-19
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    相关资源
    最近更新 更多