[英]Using a Stateful SessionScoped bean to store user login info in a JSF application
美好的一天
創建MCVE時已解決此問題(感謝Kukeltje使我做到這一點:))
有關有效的MCVE,請參見下面發布的答案。
@SessionScoped
@Stateful
public class StoringUserInfo implements Serializable {
private HashMap<String, String> userSettingMap;
private String userName = "Test";
public StoringUserInfo() {
this.userSettingMap = new HashMap<>();
}
public void addSetting(String name, String setting) {
userSettingMap.put(name, setting);
}
public String getSetting(String name) {
return userSettingMap.get(name);
}
public HashMap<String, String> getUserSettingMap() {
return userSettingMap;
}
public void setUserSettingMap(HashMap<String, String> userSettingMap) {
this.userSettingMap = userSettingMap;
}
public String getUserName() {
return userName;
}
public void setUserName(String projectName) {
this.projectName = userName;
}
}
然后例如在一個@ViewScoped @Named bean中,我想這樣做:
@Named
@ViewScoped
public class NewProjectView implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
@Inject private transient StoringUserInfo userInfo;
public void submitForm(){
userInfo.setUserName("NEW")
// return to dashboard
FacesContext.getCurrentInstance().getViewRoot().getViewMap().clear();
PrimeFaces.current().ajax().update(":content", ":formHeader:mainMenu", ":formHeader:growl");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
然后在另一個@ViewScoped @Named bean上,我想這樣檢索存儲的值(嘗試由於@PostConstruct獲取空值而抑制了應用程序部署時的錯誤)
@Named
@ViewScoped
public class EditProjectView implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
@Inject private transient StoringUserInfo userInfo;
@PostConstruct
public void init() {
getValues()
}
private void getValues(){
try {
userName = userInfo.getUserName(); // this must display on form load
} catch (Exception e) {
e.printStackTrace();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
應用程序內部的工作流程可確保在需要值的頁面之前訪問設置值的頁面。 正確調試也顯示了這一點。
但是,@ ViewScoped @Named bean始終顯示為空,它們從不從@SesionScoped bean中重新讀取值? 我在這里想念什么?
將@SessionScoped @Stateful bean中的userName字符串設置為默認值會在第二種形式上顯示名稱“ Test”。
但是,更新的值永遠不會在表單渲染/加載中讀取。 因此,似乎@PostConstruct僅在應用程序部署時執行?
我也將此添加到我的xhtml文件中:
<o:form id="editProjectForm">
<f:metadata>
<f:viewAction action="#{editProjectView.getProjectValues}"/>
<p:ajax update="editProjectFieldSet"/>
</f:metadata>
<p:fieldset id="editProjectFieldSet" legend="#{editProjectView.userName}"/>
但是它仍然不會在渲染之前重新查詢SessionScoped bean。 它僅顯示默認的初始化值“ Test”
這兩個版本之間的主要區別是:
1. Replaced <o:form> with <h:form>
應用程序在帶有PrimeFaces 6.2.9和OmniFaces 3.2的WildFly 14上運行
如果您使用的是CDI,請確保導入javax.enterprise.context.sessionscoped
而不是javax.faces.bean.sessionscoped
。
嘗試這個:
HttpSession session = request.getSession();
因此,您可以請求一個帶有名稱的參數:
String email = (String) session.getAttribute("emailsession");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.