Saving bean values ​​between views

I am looking for recommendations on preserving bean values ​​between views using JSF 2.

Consider the following scenario:
* In view A, an ABean bean scope is created and retrieves data for display in the view.
* In mind A, you can click on a record to view the details for it. B. * When you return from view B to view A, data previously retrieved by ABean is displayed.

What is the best way to save the data obtained by ABean in order to be able to display it again when returning from view B?
Retrieving data again, which usually happens because a new ABean instance is created when returning from view B, is not an option, as it is time consuming and leads to poor user experience.
If ABean is a session area, this is not an option, because if you leave the page and then return, instead of “receiving” data, “cached” data will be displayed (i.e. you cannot determine if you are loading A view as a result of navigation on page or if you are returning from view B).

What I'm looking for is obviously a conversation area that understands this perfectly (and this is what we had before when using JSF 1 and WebFlow). Unfortunately, the JSF does not have this, and since we are in a portlet environment, we cannot use CDI.

Any thoughts?

+4
source share
1 answer

Using one view with conditionally rendered content is the easiest.

eg.

<h:form> <h:dataTable id="table" value="#{bean.items}" var="item" rendered="#{empty bean.item}"> <h:column> #{item.foo} </h:column> <h:column> #{item.bar} </h:column> <h:column> <h:commandLink value="edit" action="#{bean.edit(item)}"> <f:ajax execute="@this" render="@form" /> </h:commandLink> </h:column> </h:dataTable> <h:panelGrid id="detail" columns="3" rendered="#{not empty bean.item}"> <h:outputLabel for="foo" /> <h:inputText id="foo" value="#{bean.item.foo}" /> <h:message for="foo" /> <h:outputLabel for="bar" /> <h:inputText id="bar" value="#{bean.item.bar}" /> <h:message for="bar" /> <h:panelGroup /> <h:commandButton value="save" action="#{bean.save}"> <f:ajax execute="detail" render="@form" /> </h:commandButton> <h:messages globalOnly="true" layout="table" /> </h:panelGrid> </h:form> 

with the following bean view

 private List<Item> items; private Item item; @EJB private ItemService itemService; @PostConstruct public void init() { items = itemService.list(); } public void edit(Item item) { this.item = item; } public void save() { itemService.save(item); item = null; } 

If necessary, parts of the review can be divided into two <ui:include> s. if necessary, the bean can be divided into two beans (one for each part), of which the part has a table as a managed property. But this is not necessary and only complicates the process.

+1
source

All Articles