Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger destruction of viewscoped bean?

Tags:

jsf-2

I have a @ViewScoped-annotated managedbean whose @PostContruct-method fetches a list from database to be displayed in a table in the view.
Now when I delete an item I want the changes to be seen in the view.
To keep this dynamic and reusable I only want to delete from database (not manually from list). So I need to destroy/recreate the bean I suppose. Now I do this by navigating to the same view. But the way I do is not reusable.
Can I just destroy the bean manually or navigate to the same view without explicitly navigating to THAT specific view (reusability)?

I am using JSF 2.1

like image 644
Lester Avatar asked Sep 11 '25 05:09

Lester


2 Answers

You're already on the right track. viewMap is just like any other map; You can remove a ViewScoped bean by name. Please excuse the atrocious chaining:

FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("yourBean");
like image 132
kolossus Avatar answered Sep 16 '25 11:09

kolossus


One solution I found is to destroy the bean by

FacesContext.getCurrentInstance().getViewRoot().getViewMap().clear();

I am not sure if this is the way to go because it simply destroys every viewscoped bean. It's not that bad in this case, but doesn't feel clean.
I appreciate any thought on that or alternative solutions.

like image 35
Lester Avatar answered Sep 16 '25 11:09

Lester