跟踪应用程序中的用户

发布于 2025-01-07 09:56:17 字数 513 浏览 4 评论 0原文

我需要跟踪登录到我的应用程序的用户,并且我正在尝试使用 JSF2 的 applicationMap 来实现此目的。因此,我创建了一个用户列表,并在登录 bean 中通过执行以下操作添加(删除)用户:

FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("usersList", usersList);

在 usersTracking.xhtml 页面的支持 bean 中,我尝试像这样检索列表:

AppllicationMap map = (ApplicationMap)FacesContext.getCurrentInstance().getExternalContext().getApplicationMap(); 
List users = (LinkedList)map.get("usersList");

但“users”始终为 null 。我做错了什么?

I need to track the users who are logged in to my application, and I'm trying to do this by using JSF2's applicationMap. So I create a list of users, and in my login bean I add (remove) users by doing this:

FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("usersList", usersList);

In the backing bean of the usersTracking.xhtml page I try to retrieve the list like this:

AppllicationMap map = (ApplicationMap)FacesContext.getCurrentInstance().getExternalContext().getApplicationMap(); 
List users = (LinkedList)map.get("usersList");

But "users" always comes as null. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2025-01-14 09:56:17

只需将其设为应用程序范围的托管 bean 即可。

@ManagedBean(eager=true)
@ApplicationScoped
public class Users {

    private List<User> list;

    public Users() {
        list = new LinkedList<User>();
    }

    public List<User> getList() {
        return list;
    }

}

这样你就可以通过@ManagedProperty在每个任意托管bean中注入和访问它:

@ManagedBean
@ViewScoped
public class ArbitraryBean {

    @ManagedProperty("#{users}")
    private Users users;

    // ...
}

Just make it an application scoped managed bean.

@ManagedBean(eager=true)
@ApplicationScoped
public class Users {

    private List<User> list;

    public Users() {
        list = new LinkedList<User>();
    }

    public List<User> getList() {
        return list;
    }

}

This way you can inject and access it in every arbitrary managed bean by @ManagedProperty:

@ManagedBean
@ViewScoped
public class ArbitraryBean {

    @ManagedProperty("#{users}")
    private Users users;

    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文