Beans @Inject returns null
nimo22 Aug 30, 2010 5:13 AMI use this (from the Weld-Login-Example):
@Named @RequestScoped public class Credentials { private String username; @NotNull @Size(min=3, max=25) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } private String password; @NotNull @Size(min=3, max=25) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
and my login-code:
@SessionScoped @Named public class Login{ @PersistenceContext EntityManager entityManager; @Inject Credentials credentials; private User user; public void login() { @SuppressWarnings("unchecked") List<User> results = (List<User>) entityManager .createQuery("select u from User u where u.username = :username and u.password = :password") .setParameter("username", credentials.getUsername()) .setParameter("password", credentials.getPassword()) .getResultList(); System.out.println("Username: "+ credentials.getUsername()); System.out.println("Password: "+ credentials.getPassword()); System.out.println("!results.isEmpty(): "+ !results.isEmpty()); if (!results.isEmpty()) { user = results.get(0); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Welcome, " + user.getUsername())); System.out.println("User found: "+ user.getUsername()); } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("This User does not exist.")); } }
my view:
<h:form> <div style="color: red"> <h:messages id="messages" globalOnly="true" /> </div> <h:panelGrid columns="3" rendered="#{!login.loggedIn}"> <f:validateBean> <h:outputLabel for="username">Username:</h:outputLabel> <h:inputText id="username" value="#{credentials.username}" /> <h:message for="username"/> <h:outputLabel for="password">Password:</h:outputLabel> <h:inputSecret id="password" value="#{credentials.password}" /> <h:message for="password"/> </f:validateBean> </h:panelGrid> <h:commandButton value="Login" action="#{login.login}" rendered="#{!login.loggedIn}" /> <h:commandButton value="Logout" action="#{login.logout}" rendered="#{login.loggedIn}" /> </h:form>
When clicking the Button Login
, the console prints this:
11:09:37,820 INFO [STDOUT] Hibernate: 11:09:37,820 INFO [STDOUT] select 11:09:37,820 INFO [STDOUT] user0_.ID_USER as ID1_11_, 11:09:37,820 INFO [STDOUT] user0_.firstname as firstname11_, 11:09:37,820 INFO [STDOUT] user0_.lastname as lastname11_, 11:09:37,820 INFO [STDOUT] user0_.password as password11_, 11:09:37,820 INFO [STDOUT] user0_.username as username11_ 11:09:37,820 INFO [STDOUT] from 11:09:37,820 INFO [STDOUT] SCHEMA.USER user0_ 11:09:37,820 INFO [STDOUT] where 11:09:37,820 INFO [STDOUT] user0_.username=? 11:09:37,820 INFO [STDOUT] and user0_.password=? 11:09:37,883 INFO [STDOUT] Username: null 11:09:37,883 INFO [STDOUT] Password: null 11:09:37,883 INFO [STDOUT] !results.isEmpty(): false
So you see, the injection of Credentials in Bean Login
does not work.
Why?
(I am using JBOSS 6 M4 with actual CDI, JSF2).