1

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.

Community
  • 1
  • 1
karolkpl
  • 2,189
  • 10
  • 39
  • 60
  • The server does not know if you are opening the page in a new window or the same, so it interprets that your view has changed when opening the new page. Posted as comment because I do not know how you can workaround it (other than making the offending bean `@SessionScoped`. – SJuan76 Dec 12 '12 at 14:44

2 Answers2

2

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");
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

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.

djmj
  • 5,579
  • 5
  • 54
  • 92