我有一个奇怪的问题,我使用的是JSF 2.1.6和Primefaces 3.4。
问题是,我的@ViewScoped bean中的@PostConstruct在onSelectNavigate之后再次触发,这导致了NPE,因为在Flash作用域中不再设置东西。
我有以下init()方法
@PostConstruct public void init() {
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");
}然后,我在日志中看到的是,导航方法被调用,但随后同一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发布于 2012-10-17 19:16:59
您正在导航到不同的视图。这就是当前视图范围的末尾。如果由于某种原因,相同的支持bean类也被引用为另一个视图中的视图作用域bean,那么将为新的视图作用域创建一个新实例。
具体的功能需求并不明确,因此不可能为您尝试实现的任何目标提出正确的方法。也许“修复”这个问题的最简单方法是在@PostConstruct中添加一个空检查。
https://stackoverflow.com/questions/12932977
复制相似问题