JSF 2.2 Resource Injection Issues

I have been trying to achieve resource injection

for a long time but have not been able to succeed.

I am using JSF 2.2

, JDK 1.7.

AND my ide eclipse luna

.

I have a session bean called UserBean

and view the scope of the bean called SettingsBean

.

I have set them to faces-config.xml

UserBean like session scoped

SettingsBean like view scoped

with their name bean " SettingsBean

" and " UserBean

"

public class SettingsBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject  // I also tried @ManagedProperty but didn't work
    private UserBean userBean;

    @PostConstruct
    public void init(){         
        System.out.println(userBean.getUser().getFullName());
    }

   public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

}

      

The problem is I am getting userBean as null. What is the problem? Thanks for the help.

+3


source to share


1 answer


I removed the definition ManagedBean

and ViewScoped

in the faces-config.xml for settingsBean

adding them to a file SettingsBean.java

manually.

And added also:

@ManagedProperty(value="#{userBean}")   
    private UserBean userBean;  

      



So it works:

    @ManagedBean
    @ViewScoped
    public class SettingsBean implements Serializable{

        private static final long serialVersionUID = 1L;

        @ManagedProperty(value="#{userBean}")   
        private UserBean userBean;  
        //...
 @PostConstruct
    public void init(){         
        System.out.println(userBean.getUser().getFullName());
    }
    public UserBean getUserBean() {
            return userBean;
        }

        public void setUserBean(UserBean userBean) {
            this.userBean = userBean;
        }
}

      

+2


source







All Articles