java ee - 重定向到新窗口NPE后的JSF 2.0 ViewScoped Bean [英] java ee - JSF 2.0 ViewScoped Bean after redirect to new window NPE

查看:134
本文介绍了java ee - 重定向到新窗口NPE后的JSF 2.0 ViewScoped Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 https://stackoverflow.com/a/13838907 上的提示打开新标签,但是当我回到旧版本我得到nullPointerException并且我的ViewScoped bean数据丢失了。

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>

单击第一个按钮,返回上一个选项卡,单击第二个按钮。再次创建PageBean,所有数据都将丢失。这两个bean都是ViewScoped。

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.

推荐答案

实际上,初始标签/窗口中的视图范围bean会因返回<$ c而被杀死$ c>字符串导航案例结果。您希望返回 null void 以使其保持活动状态。根据您在其他问题中显示的 newTab()代码,您需要用 Faces#redirect() 调用(假设它确实是 OmniFaces ,你在那里使用 Faces #setFlashAttribute( ))。您只需要预先将 Flash #setRedirect()设置为 true ,以指示Flash范围将发生重定向。

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

这篇关于java ee - 重定向到新窗口NPE后的JSF 2.0 ViewScoped Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆