Reset JSF Backing Bean(View or Session Scope)

前端 未结 5 1145
南方客
南方客 2020-12-14 04:36

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

相关标签:
5条回答
  • 2020-12-14 04:57

    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()"/>
    
    0 讨论(0)
  • 2020-12-14 04:57

    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

    0 讨论(0)
  • 2020-12-14 05:07

    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).

    0 讨论(0)
  • 2020-12-14 05:11

    I found the solution for View scope.

        public static void removeViewScopedBean(String beanName) 
        {
          FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(beanName);
        }
    
    0 讨论(0)
  • 2020-12-14 05:15

    just clear all views:

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

    and remember to implements Serializable in all views

    0 讨论(0)
提交回复
热议问题