3
votes

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

3

3 Answers

5
votes

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");
2
votes

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.

-1
votes

When you return a non null value inside a method from an action attribute the Bean gets recreated.

index.xhtml

...
<h:commandButton value="delete" action="bean.delete" />
...

Bean.class

...
public String delete() {
  // do operations
  return "index.xhtml?faces-redirect=true";
}
...