This is how I have implemented Flash scope in pages to ensure bean is available after redirection. I am using JSF 2.1.19 shipped with JBoss EAP 6.
page1.xhtml
<h:commandLink value="#{result.itemVO.number}" action="#{itemBean.goToSecondPage}">
<f:setPropertyActionListener target="#{itemBean.itemVO}" value="#{result.itemVO}" />
</h:commandLink>
ItemBean.Java
This is common bean between both pages.
public String goToSecondPage() {
Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
flash.put("itemBean", this);
flash.setKeepMessages(true);
flash.keep("itemBean");
return "secondPage";
}
@PostConstruct
public void init() {
Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
ItemBean itemBean = (ItemBean) flash.get("itemBean");
//Number
itemBean.getItemVO.getNumber();
}
itemBean object is available from flash in init method after redirection from page1.
But when I do page reload on page 2, flash is empty even if I call setKeepMessages(true). There are lots of fields which I load on the page based on the itemVO but that is not present after reload and the page is rendered blank. Either I want to capture reload event and stop rendering of the page OR I need find a way to persist Flash object in case of reload.
I have gone through this post to persist the flash object after reload, but it talk about use of EL. But In my case I need to access it in bean post construct method.
I also tried navigation timing api to capture refresh event in javascript as explained here. But this is called after bean initialization, which I want to avoid due to some legacy code issues.