java ee - JSF 2.0 ViewScoped Bean after redirect to new window NPE

谁说胖子不能爱 提交于 2019-12-11 11:43:48

问题


I use tip from this post https://stackoverflow.com/a/13838907 to open new tab, however when I go back to old one I get nullPointerException and my ViewScoped bean data are lost.

<h:form target="_blank">
  <p:commandButton value="open new tab" action="#{otherBean.newTab}" ajax="false" />
</h:form>

<h:form>
  <p:commandButton value="this wll cause NPE" action="#{pageBean.action}"/>
</h:form>

Click first button, go back to previous tab, click second button. PageBean is created once again and all data are lost. Both beans are ViewScoped.


回答1:


Indeed, the view scoped bean in the initial tab/window get killed by returning a String navigation case outcome. You would like to return null or void to keep it alive. Based on the newTab() code as shown in your other question, you need to replace the navigation case by a Faces#redirect() call (assuming that it's indeed OmniFaces which you're using there for Faces#setFlashAttribute()). You only need to set Flash#setRedirect() to true beforehand to instruct the flash scope that a redirect will occur.

public void newTab() throws IOException {
    Faces.setFlashAttribute("foo", bar); 
    Faces.getFlash().setRedirect(true);
    Faces.redirect("otherView.xhtml");
}



回答2:


ViewScope beans only live as long as you post back to same view.

If you post back to other views in your action data will be lost since the ViewScope bean will be recreated.



来源:https://stackoverflow.com/questions/13842036/java-ee-jsf-2-0-viewscoped-bean-after-redirect-to-new-window-npe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!