After switching from JSF @ManagedBean to CDI @Named, the constructor called several times, and the presented input values ​​were always empty

Edit: the comments section solved my problem! The problem was that I used the wrong import for Scopes.

I have a simple JSF application (login, pull data from the database, allow the user to edit the data). It works well, I want to update the code to use CDI (Weld), but I'm having problems. I follow / look at: http://docs.jboss.org/weld/reference/latest/en-US/html/example.html Original materials without welding:

login.xhtml

<h:form id="inputForm">
    <h:panelGrid columns="2" cellpadding="5" cellspacing="1">
        <h:outputText id="nameDesc" value="Name"></h:outputText>
        <h:inputText id="nameInput" value="#{login.loginName}" binding="#{name}"></h:inputText>

        <h:outputText id="passwordDesc" value="Password"></h:outputText>
        <h:inputSecret id="passwordInput" value="#{login.password}" binding="#{password}"></h:inputSecret>

    </h:panelGrid>
    <h:commandButton value="Login" action="#{login.login(name.value, password.value)}"/>
</h:form>

LoginBean.java:

@ManagedBean(name="login")
@SessionScoped
public class LoginBean implements Serializable {
    private static final long serialVersionUID = 1L;
    @ManagedProperty(value="#{db}")
    private DatabaseBean db;
    private String password;
    private String loginName;
    // other stuff and functions

    public String getLoginName () {
       return loginName;
     }

    public void setLoginName (String name) {
       this.loginName = name;
    }

    public String getPassword () {
       return password;
    }

    public void setPassword (final String password) {
       this.password = password;
    }

    public void setDb(DatabaseBean db) {
        this.db = db;
    }

DatabaseBean.java:

@ManagedBean(name="db", eager=true)
@ApplicationScoped
public class DatabaseBean implements Serializable {
 @PostConstruct
    public void init() {
      //... connect to database etc
    }    

}

--------- That I tried to get it to work with Weld (only the changes on top to make it a little shorter): -------- LoginBean.java, changed to @Named from @ManagedBean, added @Inject for DatabaseBean

@Named("login")
@SessionScoped
public class LoginBean implements Serializable {
     // stuff 
         private @Inject DatabaseBean db;
}

DatabaseBean.java, @Named @ManagedBean:

@Named("db")
@ApplicationScoped
public class DatabaseBean implements Serializable {
}

LoginBean :

public String login(String name, String password) {
    System.out.println("login called"+name);
    // other stuff

}

(, Weld) : "login called", ( .IsEmpty()).

:

loginBean.java

@Inject
public LoginBean(DatabaseBean db) {
    System.out.println("constructor");
    this.db = db;
}

, "" , , , - , , LoginBean ( ), - . ?

Eclipse Tomcat8 . , !

+4
1

bean,

CDI , , / /. . Field.get(obj) NULL CDI beans, getters . , . @PostConstruct - .


: "login called", ( .IsEmpty()).

null , , , @SessionScoped CDI bean, , , a @Dependent bean. , bean CDI. . bean JSF 2?

, , , @SessionScoped . , javax.enterprise.context, , , javax.faces.bean. JSF bean bean, CDI.

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;

@Named("login")
@SessionScoped
public class LoginBean implements Serializable {
    // ...
}
+2

All Articles