当我运行我的项目时,Spring 在部署时(我认为是在 spring bean 创建期间)调用 JSF Action Bean
方法的 PostConstruct
方法。
正如我所想,部署时没有 FacesCOntext
,我在 PostConstruct
方法中收到 NullPointerException
。
import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;
@Named
@ViewScoped
public class AddStudentActionBean implements Serializable {
private Student student;
@PostConstruct
public void init() {
student = (Student) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Student");
}
@PreDestroy
public void destory() {
}
}
如何解决?
最佳答案
只是不要让Spring
创建AddStudentActionBean
类的bean。我的意思是,这样做没有任何意义。 CDI
容器将在需要时创建和管理 AddStudentActionBean
bean。
通常,您希望在 Spring
的上下文中创建 Service
或 DAO
bean。
关于java - JSF 2.0 CDI + Spring 3.1 集成,FacesContext 在部署时为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21281803/