[英]JSF PrimeFaces lose data and session
我正在使用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。 當我檢查用戶是否已登錄時,如果他是,則啟動Home。 Home正確顯示用戶名,但在家中我使用searchCB登陸SearchResults頁面時不會顯示用戶名。
有人可以幫忙嗎?
import javax.enterprise.context.SessionScoped;
您為會話范圍導入了錯誤的注釋。 如果您正在使用JSF @ManagedBean
,那么您需要從javax.faces.bean
包中導入范圍。 以上僅適用於CDI @Named
。
所以,相應地修復它:
import javax.faces.bean.SessionScoped;
沒有正確范圍的@ManagedBean
將表現為@NoneScoped
。 即每個EL評估都會創建一個新實例,這正是您所看到的問題行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.