简体   繁体   English

重置JSF Backing Bean(视图或会话范围)

[英]Reset JSF Backing Bean(View or Session Scope)

I want to reset by JSF backing bean when some method is invoked. 我想在调用某个方法时通过JSF支持bean重置。 Assume that there is a command button, someone press it and after succesfull transaction, my View or Session scope JSF bean should be reseted. 假设有一个命令按钮,有人按下它,在成功完成事务后,我的View或Session范围JSF bean应该被重置。 Is there a way to do that? 有没有办法做到这一点?

Thank 谢谢

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. 当您从操作方法返回非null或非void时,将重新创建视图范围bean,即使它将返回到同一视图。 So, just return a String from the action method, even if it's just an empty string: 所以,只需从action方法返回一个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. 要使其完整,您可以考虑通过将?faces-redirect=true查询字符串附加到返回的结果来发送重定向。

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. 对于您目前正在尝试实现的任何内容,会话范围的bean首先是错误的范围。 The bean in question should have been be a view scoped one. 有问题的bean应该是一个视图范围的bean。 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). 忽略这一点,您可以在action方法中重新创建模型,或者可能完全使会话无效(这也会破坏所有其他视图和会话范围的bean,但不确定这是否是您所追求的)。

I found the solution for View scope. 我找到了View范围的解决方案。

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

just clear all views: 只是清除所有意见:

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

and remember to implements Serializable in all views 并记住在所有视图中实现Serializable

您还可以从javascript刷新页面,因此ViewScoped Bean将被重置,例如在primefaces commandButton中:

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

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 通过这种方式,我进入会话范围的bean并重置它没有之前的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM