【问题标题】:Re-create session scoped JSF managed bean programmatically以编程方式重新创建会话范围的 JSF 托管 bean
【发布时间】:2015-08-22 18:18:57
【问题描述】:

我正在使用 Mojarra 2.1.29 并且有一个会话范围的 JSF 托管 bean

@ManagedBean
@SessionScoped
public class UserContext{

   //staff

}

还有一颗春豆:

public class UserReproducer{
    private User user;

    public void reporoduce(){
        if(user == null){
            //Here I need to recreate the userContext managed bean
            //do some other staff
        }
    }
}

事实上,我需要 JSF 中的某种自定义范围,即在满足条件时从头开始重新创建 userContext bean。有可能以某种方式做吗?通过重新创建,我的意思是在第一次创建时清除它的所有属性。

【问题讨论】:

    标签: jsf jsf-2 managed-bean session-scope recreate


    【解决方案1】:

    使会话无效,以销毁会话范围的 bean:

    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    

    另一个退出 Spring Security 的选项是清除上下文:

    SecurityContextHolder.clearContext();
    

    【讨论】:

    • 该会话是否与 Spring Security 的会话相关联?我的意思是那是一回事吗?
    • SecurityContext 对象保存在 http 会话中是的。
    • 当用户尝试登录时,是否有可能使 JSF 会话无效?
    • 为什么不给类一个reset()方法?
    • 此解决方案使整个 HTTP 会话无效。因此,它不仅清除 bean,而且使所有会话中的数据无效,并查看链接到该 HTTP 会话的托管 bean。对于具体问题,这不是最合适的解决方案,OP 的要求可以通过@Kukeltje 的解决方法来完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2012-12-26
    • 1970-01-01
    • 2012-04-16
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多