Ahmet DAL
Ahmet DAL

Reputation: 4692

Reset JSF Backing Bean(View or Session Scope)

I want to reset by JSF backing bean when some method is invoked. Assume that there is a command button, someone press it and after succesfull transaction, my View or Session scope JSF bean should be reseted. Is there a way to do that?

Thank

Upvotes: 11

Views: 30433

Answers (5)

Luis Manrique
Luis Manrique

Reputation: 306

I solve the problem with code like this:

((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().removeAttribute("bean name");            

By this way I enter to session scoped bean and reset it without the data that was there before

Upvotes: 3

user3204899
user3204899

Reputation: 61

You could also refresh the page from javascript, so the ViewScoped Bean will be reseted, for example in a primefaces commandButton:

<p:commandButton value="Button" action="#{bean.someAction()}" oncomplete="location.reload()"/>

Upvotes: 3

John Ricaurte
John Ricaurte

Reputation: 45

just clear all views:

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

and remember to implements Serializable in all views

Upvotes: 3

BalusC
BalusC

Reputation: 1108982

A view scoped bean will be recreated when you return non-null or non-void from the action method, even if it would go back to the same view. So, just return a String from the action method, even if it's just an empty string:

public String submit() {
    // ...

    return "";
}

To make it complete, you could consider sending a redirect by appending the ?faces-redirect=true query string to the returned outcome.

public String submit() {
    // ...

    return "viewId?faces-redirect=true";
}

A session scoped bean is in first place the wrong scope for whatever you're currently trying to achieve. The bean in question should have been be a view scoped one. Ignoring that, you could just recreate the model in the action method, or very maybe invalidate the session altogether (which would only also destroy all other view and session scoped beans, not sure if that is what you're after though).

Upvotes: 13

Ahmet DAL
Ahmet DAL

Reputation: 4692

I found the solution for View scope.

    public static void removeViewScopedBean(String beanName) 
    {
      FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(beanName);
    }

Upvotes: 15

Related Questions