2

I am creating a web application using JSF 2.2.20 in which I am implementing a "kinda wizard" flow which lets the user filling input fields and go back and forth the view pages through navigation. I am using a single bean for all these views.

Let's say I have views A.xhtml, B.xhtml, C.xhtml and D.xhtml, all managed by the same bean MyBean.java

I want my application to be "browser tab scoped", which means that

  1. I do not want my bean's data be re-instantiated after every HTTP Request as it happens with @RequestScoped beans or after view changing as it happens with @ViewScoped, I want the data of my bean to be kept between view changes and redirections so the user can go back and forth between pages without losing the data he has already given.
  2. I do not want to use the @SessionScoped scope since each time the user opens a new tab I want the bean to be re-instantiated starting from page "A.xhtml.

Is there any built-in way to achieve the scenario described above using the current JSF version? In case there is not any, could you please propose any workarounds?

Thanks in advance!

NickAth
  • 1,089
  • 1
  • 14
  • 35
  • Does this answer your question? [How to choose the right bean scope?](https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Jasper de Vries Apr 12 '22 at 05:27

1 Answers1

1

I think @ViewScoped is what you are looking for, but it depends on your exact usage.

Couple of notes:

  • Use javax.faces.view.ViewScoped. Don't use the deprecated managed bean annotation as it works differently.
  • @ViewScoped works by storing the beans in the view. So each time you load the page you get a view and a viewId that corresponds to that view. So effectively each load of the page (could be read as 'each browser tab') gets its own bean.
  • @ViewScoped is a passivating scope. That means your beans, and their injected Dependencies, do need to be Serializable.
  • Use a recent, up-to-date version of your app server, or if you bring in MyFaces manually, use the latest release. I found a number of older versions implementations buggy 5+ years ago, but it seems to work flawlessly now.

If there is a Page Navigation occurring, you probably want to use FlowScoped. This is a multi-page bean that stays alive until you end the 'flow'.

If neither of these two work, you can always implement your own scope which is surprisingly easy with CDI.

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • Hi Jonathan, thanks for your answer! I am sorry I did not make it crystal clear in my question that I am using navigation through pages so the `@ViewScoped` scope is not an option that fits for my case (I updated my question). Concerning the `FlowScoped` scope, could you please provide me with some more details, any example or any resource? I have seen that scope as an option but I have not found any enlighting resources yet about that – NickAth Apr 11 '22 at 16:19
  • A goog search brought up this: https://www.byteslounge.com/tutorials/jsf-flow-example – Jonathan S. Fisher Apr 11 '22 at 16:29
  • Thank you for that! I downloaded the zip file from the resource but I am having many building issues to solve to deploy this app, also tried to apply the @FlowScoped example of the resource into my application but I also faced building issues, (my application is a spring boot application integrated with JSF) :/. I should dive deeper to solve my building/dependencies issues I guess – NickAth Apr 12 '22 at 08:52
  • Try importing myfaces-api and myfaces-impl – Jonathan S. Fisher Apr 13 '22 at 14:50