0

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.

priyam
  • 74
  • 7
  • 1
    What is your functional problem that you want to prevent with these technical attempts? – Kukeltje Nov 16 '18 at 13:47
  • managed bean is getting re-initialized after refresh of the page though the bean is in view scope. This causes improper loading of page. – priyam Nov 19 '18 at 04:27
  • [mcve] the and version info since this can have several cause all of which have existung duplicate Q/A in stackoverflow – Kukeltje Nov 19 '18 at 07:14
  • @KukeltjeI have edited the question to give more details. And also have gone thru other articles but none seems to resolve my issue. – priyam Nov 20 '18 at 03:50
  • Doing a refresh (reload) is a new 'HTTP GET' so effectively a new view *instance* (although the same view), so a new viewscoped bean is used. If you want to have it survive requests, so 'keep alive when accessed in a new view' (new instance of the same view or even a new page) you can (should) use the DeltaSpike [`@ViewAccessScoped`](https://deltaspike.apache.org/documentation/jsf.html#@ViewAccessScoped) and then use normal `@Inject` into your bean. – Kukeltje Nov 20 '18 at 08:20
  • does it mean using only JSF and Javasript, I can not keep alive my bean for page reload? – priyam Nov 20 '18 at 11:30
  • Sure you can if you add the required info as url parameters, Flashscope is not meant for this. And using DeltaSpike and OmniFaces is a good choice to begin with. Saves you a lot of 'util' classes and adds additional great features. – Kukeltje Nov 20 '18 at 13:51

0 Answers0