0

Lets say I have a @ViewScoped Bean behind my current page A. Now the user navigates to page B via a normal get request, lets say to www.google.com. When the user clicks the back button of the browser, I would like to restore the @ViewScope of the previous page, so that it appears exactly as it was left. Is that possible to achieve somehow?

I dont want to make my page A @SessionScoped so that the backing beans do not disturb each others state when opened in two browser tabs.

Thomas
  • 620
  • 7
  • 19
  • In case of Ajaxical requests, there is no question of restoring - JSF or otherwise. In other cases, if page navigation is done via HTTP POST requests, then the whole concept of page navigation used is wonky and needs to be reconsidered providing necessary request parameters using the HTTP GET request (page navigation with corresponding bookmarkable URLs in any web project always takes place using the HTTP GET request). – Tiny Jul 21 '17 at 03:23
  • Persist values in @PreDestroy(), and get them back in @PostContruct() method. Or check that link https://stackoverflow.com/questions/1282251/saving-data-to-session-in-jsf to see how to store them in session. – stakahop Jul 21 '17 at 12:29

2 Answers2

0

Since version 2.6 OmniFaces has this feature, is called @ViewScoped(saveInViewState = true) But with some caution!

It's very important that you understand that this setting has potentially a major impact in the size of the JSF view state, certainly when the view scoped bean instance holds "too much" data, such as a collection of entities for a data table, and that such beans will in fact never expire as they are stored entirely in the javax.faces.ViewState hidden input field in the HTML page. Moreover, the @PreDestroy annotated method on such bean will explicitly never be invoked, even not on an unload as it's quite possible to save or cache the page source and re-execute it at a (much) later moment.

A more programmatical solution is the @ConversationScoped. With the convertsation id as parameter can you restore the view. conversationscope example

jklee
  • 2,198
  • 2
  • 15
  • 25
  • Thank you for your suggestion. I am not sure about the implications of this approach but i will look into it. – Thomas Aug 15 '17 at 08:52
0

Yes it is possible, pass parameter like this using f:param this will pass your parameter to the next screen.

    <h:commandLink action="screenName" value="#{search.participantName}">
    <f:param value="#{searchcus.participantId}" name="PARTICIPANT_ID"/>
    <f:param name="PARENT_SCREEN_CODE" value="SEARCH_PARTICIPANT"/>
    </h:commandLink>

After that in init() method get value as a parameter to fetch the result.