First of all, I am using OmniFaces CDI @ViewScoped which was recently added to and released in OmniFaces 1.6. I am very pleased to use OmniFaces CDI @ViewScoped in my app, but I have a question.
I noticed some NullPointerException's in my (TomEE 1.6 snapshot) server log, and I even experienced the NullPointerException's while testing pages that use/reference beans marked with OmniFaces CDI @ViewScoped. The NullPointerException's occur when I typically do something similar to the following:
(1) 'Render' a page that references/uses CDI @ViewScoped bean.
(2) Click a (PrimeFaces) commandButton/Link (below) which exists the 'view', and the commandButton/Link action="..." is responsible for destroying the @ViewScoped bean.
<p:commandButton value="Exit Messenger"
action="#{messengerBean.exitMessenger()}"
ajax="false"/>
and
public String exitMessenger() {
pageNavigationController.setToBlankPage();
// to destroy this CDI @ViewScoped bean
return "index.xhtml";
}
(3) Immediately after step #2 above, myself or enduser does a 'browser refresh' (F5 key in Google Chrome), and for some/whatever reason, the CDI @ViewScoped bean (referenced by step #1 above) is reconstructed without the bean being reconstructed...correctly, since an actionListener=#{viewScopedBean.methodToPrepareStep1View()} or action="#{viewScopedBean.methodToPrepareStep1View()}" is 'not' invoked by the 'browser refresh' button/request.
(4) So, the @ViewScoped bean members are 'null' and NullPointerException's are being raised when users 'unexpectedly' press F5 or do a 'browser refresh'.
NOTE: state saving = server and (HTTP) Filter is instructing browser 'not' to cache the jsf/xhtml page via the following (which I learned from BalusC, long time ago, here on stackoverflow.com):
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
res.setDateHeader("Expires", 0); // Proxies.
Also, I read the following other/related posts:
Is JSF 2.0 View Scope back-button safe?
What scope to use in JSF 2.0 for Wizard pattern?
ViewScoped works like RequestScoped - why?
Is this due to bad design on my part (as 2-year-old JSF developer)?
Do I need to test all of my xhtml pages which reference CDI @ViewScoped beans, and repeat the steps that I listed above, and then just handle NullPointerException everywhere in my code...that is caused by 'browser refresh'? Is this the only way to resolve this or is there a better more-recommended way of handling (or avoiding) NullPointerException's caused by browser refresh after CDI @ViewScoped bean destroyed?
Please advise/confirm. Thanks.
EDIT: (2) above with 'correct' description and code that helps to replicate this issue.