简体   繁体   中英

JSF2 Can't reach SessionScoped bean from ViewScoped as ManagedProperty

I have a strange problem. Afaik I can inject a SessionScoped bean into a viewscoped, because its broader, than the other. Here is my code:

@ManagedBean
@ViewScoped
public class ProjectBean implements Serializable {

@ManagedProperty(value="#{projectCurrentBean}")
private ProjectCurrentBean currentBean;

public void setCurrentBean(ProjectCurrentBean currentBean) {
    this.currentBean = currentBean;
}     

@ManagedProperty(value="#{userCredentialsBean}")
private UserCredentialsBean activeUser;

public void setActiveUser(UserCredentialsBean activeUser) {
    this.activeUser = activeUser;
}

The 2 managed bean:

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable  {

and

@ManagedBean
@SessionScoped
public class UserCredentialsBean  implements Serializable {

It works fine with the UserCredentialsBean, but when I put the ProjectCurrentBean it fails:

Unable to create managed bean projectBean. The following problems were found: - The scope of the object referenced by expression #{projectCurrentBean}, request, is shorter   than the referring managed beans (projectBean) scope of view

why? :)

You've not declared the bean using @SessionScoped from javax.faces.bean package , but instead from javax.enterprise.context package . This don't work in combination with @ManagedBean from javax.faces.bean package. The bean will then default to the request scope and behave like @RequestScoped .

Fix your imports.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class ProjectCurrentBean implements Serializable {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM