gpt4 book ai didi

java - 通过 FacesContext 获取 SessionScoped Bean 的字段

转载 作者:行者123 更新时间:2023-12-04 06:06:53 25 4
gpt4 key购买 nike

我有以下类(class):

A类

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import timereport.db.UserBean;
import timereport.utils.JPAUtil;

@Named("classA")
@SessionScoped
public class ClassA implements Serializable {

@Inject
protected UserBean userBean;

public void logout() {
userBean = null;
}

public void login() {
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());
}

//setter and getter for userBean
}

在这里 login()我在做
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());

获取整个 UserBean对象,这是正确的。问题来了……

B类
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.sql.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import timereport.db.UserBean;
import timereport.ClassB;
import timereport.utils.JPAUtil;

@Named("classB")
@SessionScoped
public class ClassB implements Serializable {

@Inject private UserBean userBean;
String throughUserBean = userBean.getUsername();
...
}

在这里,我不知道为什么,但我希望在用户登录时设置用户名。当我调用 @Inject UserBean 或 @Inject ClassB 时,我希望它们来自同一个 session 范围并持有 UserBean 对象在登录时初始化。但是当我引用它们时(classB 和 userBean)都返回 NULL。我对设置这些对象的期望是错误的吗?还有其他方法吗?

这是 UserBean 类的一部分:
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


import com.proxiad.timereport.utils.AESEncryptDecryptUtil;

@Named("user")
@SessionScoped
@Entity
@Table(name="t_user")
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = -56986575421886097L;
@Id
@Column(name="username")
private String username;
@Column(name="fullname")
private String fullname;
@Column(name="password")
private String password;
@Column(name="email")
private String email;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn (name="department", referencedColumnName="value")
private ReferenceDataBean department;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="role", referencedColumnName="value")
private ReferenceDataBean role;

public UserBean() {}

public String getFullname() {
return fullname;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public String getUsername() {
return username;
}

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

public String getPassword() {
return password;
}

public void setPassword(String password) {
try {
this.password = AESEncryptDecryptUtil.encrypt(password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public ReferenceDataBean getDepartment() {
return department;
}

public void setDepartment(ReferenceDataBean department) {
this.department = department;
}

public ReferenceDataBean getRole() {
return role;
}

public void setRole(ReferenceDataBean role) {
this.role = role;
}

@Override
public int hashCode() {
return getUsername().hashCode() + 17 * getEmail().hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof UserBean) {
UserBean user = (UserBean) obj;
if (getUsername().equals(user.getUsername()) && getEmail().equals(user.getEmail())) {
return true;
}
}
return false;
}

@Override
public String toString() {
return String.format("Username: %s\nEmail: %s\nDepartment:\n%s\nRole:\n%s\n",
getUsername(), getEmail(), getDepartment(), getRole());
}

}

最佳答案

注入(inject)发生 之后 build 。注入(inject)的属性在施工期间不可用。访问注入(inject)属性的最早点是 @PostConstruct方法。

所以,更换

@Inject private UserBean userBean;
String throughUserBean = userBean.getUsername();

经过
@Inject private UserBean userBean;
String username;

@PostConstruct
public void init() {
username = userBean.getUsername();
}

@Inject ClassB没有意义,所以我删除了它。

关于java - 通过 FacesContext 获取 SessionScoped Bean 的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com