JSF PrimeFaces 丢失数据和会话

发布于 2024-12-26 10:14:55 字数 2243 浏览 1 评论 0原文

我正在使用 JSF 和 PrimeFaces 开发一个应用程序。 我有一个托管的会话范围,有用户名、密码和 isUserLoggedIn。 当我处理登录组件时,它会工作并相应地更改我的页面。一旦我移动到另一个页面,我就会丢失用户名数据。我需要在整个申请过程中访问用户名。 有谁知道为什么我会丢失应在 eb 会话范围内的数据?为什么我将其保留在一页而不是其他页面上? 感谢

import authentication.AuthenticatorManagerLocal;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@SessionScoped
public class UserMB {
    @EJB
    private AuthenticatorManagerLocal authenticatorManager;

    /** Creates a new instance of UserMB */
    public UserMB() {
    }

    Boolean isUserLoggedIn;
    String username;
    String password;
    String nickName;

    public String getNickName() {
        nickName="vanessa";
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Boolean getIsUserLoggedIn() {
        return isUserLoggedIn;
    }

    public void setIsUserLoggedIn(Boolean isUserLoggedIn) {
        this.isUserLoggedIn = isUserLoggedIn;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String authenticateUser(){
       isUserLoggedIn= authenticatorManager.authenticateUser(username, password);
       if(isUserLoggedIn)return "Home";
       else
           return null;
    }

    public void logout(){
        isUserLoggedIn=false;
        username="";
        password="";
    }

    public String goToIndex(){
        return "Index";
    }

}

HOME 内部有

<p:commandButton value="SearchCB" action="#{expSearchResultsMB.search()}" ajax="false" />  

一个自定义组件

expSearchResultsMB.search() 发送到 SearchResults 我想

 <h:outputLabel value="#{userMB.username}" /> 

在应用程序的每个页面中显示用户名,我需要访问用户名和 isUSERLoggedin 。 当我检查用户是否登录时,如果登录,我会启动主页。 主页正确显示了用户名,但是当我在家里使用 searchCB 时,登陆 SearchResults 页面不显示用户名。

有人可以帮忙吗?

I am developing an application with JSF and PrimeFaces.
I have a managed been, session scoped, that has username, password and isUserLoggedIn.
When I process a login component it works and changes my page accordingly. As soon as I move to another page I lose the data the username data. I need to access username during the entire application.
Does anyone know why I lose the data which should eb session scoped? why do I keep it from one page and not for the others?
Thanks

import authentication.AuthenticatorManagerLocal;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@SessionScoped
public class UserMB {
    @EJB
    private AuthenticatorManagerLocal authenticatorManager;

    /** Creates a new instance of UserMB */
    public UserMB() {
    }

    Boolean isUserLoggedIn;
    String username;
    String password;
    String nickName;

    public String getNickName() {
        nickName="vanessa";
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public Boolean getIsUserLoggedIn() {
        return isUserLoggedIn;
    }

    public void setIsUserLoggedIn(Boolean isUserLoggedIn) {
        this.isUserLoggedIn = isUserLoggedIn;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String authenticateUser(){
       isUserLoggedIn= authenticatorManager.authenticateUser(username, password);
       if(isUserLoggedIn)return "Home";
       else
           return null;
    }

    public void logout(){
        isUserLoggedIn=false;
        username="";
        password="";
    }

    public String goToIndex(){
        return "Index";
    }

}

HOME has

<p:commandButton value="SearchCB" action="#{expSearchResultsMB.search()}" ajax="false" />  

inside a custom component

expSearchResultsMB.search() sends to SearchResults
where I want to display the username

 <h:outputLabel value="#{userMB.username}" /> 

I need to access username and isUSerLoggedin in every page of the application.
When I check if the user is logged in I launch Home if he is.
Home shows the username correctly, but when from home I use searchCB the landing SearchResults page doesn't show the username.

Can anyone help?

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

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

发布评论

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

评论(1

筱果果 2025-01-02 10:14:55
import javax.enterprise.context.SessionScoped;

您为会话范围导入了错误的注释。如果您使用 JSF @ManagedBean,那么您需要从 javax.faces.bean 包导入范围。以上仅适用于 CDI @Named

因此,请相应地修复它:

import javax.faces.bean.SessionScoped;

没有正确作用域的 @ManagedBean 将表现为 @NoneScoped。即,每次 EL 评估都会创建一个新实例,这正是您所看到的有问题的行为。

import javax.enterprise.context.SessionScoped;

You've imported the wrong annotation for the session scope. If you're using JSF @ManagedBean, then you need to import the scopes from the javax.faces.bean package. The above is for CDI @Named only.

So, fix it accordingly:

import javax.faces.bean.SessionScoped;

A @ManagedBean without the right scope would behave as @NoneScoped. I.e. a new instance will be created on every EL evaluation which is exactly the problematic behaviour you're seeing.

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