我有一个奇怪的问题,我使用的是JSF 2.1.6和Primefaces 3.4。
问题是我的@ViewScoped bean中的@PostConstruct在onSelectNavigate后再次触发,导致NPE,因为Flash Scope中不再设置东西。
我有以下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");
}
我在日志中看到的是调用了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
答案 0 :(得分:0)
您正在导航到其他视图。这是当前视图范围的结束。如果由于某种原因,同一个支持bean类在另一个视图中也被引用为视图范围bean,那么将为新视图范围创建一个新实例。
具体的功能要求尚不清楚,因此无法为您想要达到的目标提出正确的方法。也许解决这个问题的最简单方法是在@PostConstruct
中添加一个nullcheck。