Injecting doesn't work
newlukai Feb 7, 2006 4:52 AMHi folks,
I've searched the forum, but I didn't find a topic solving my problem.
I modified the booking example to test the capabilities of Seam. I have to say, Seam is very nice.
As I said, I modified the booking example. I left the LoginAction as it was. After logging in, the user sees a personalized welcome message. The Name is retrieved from a database. It works fine. The User enters his token and is welcomed with his name.
A panelNavigation2 (nested in a f:view) displays some sort of navigation in the sidebar. As you see, I use myFaces. One of the navigation Items calls a method in a stateless bean, where I want the user component to be injected. But this doesn't work. Debugging this code reveals that as soon as the called method is invoked, user equals null. I don't know, what's wrong with my code. It's similar to the booking example (i.e. ChangePasswordAction).
User.java:
package myprog.entities; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.validator.Length; import org.hibernate.validator.NotNull; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import static org.jboss.seam.ScopeType.SESSION; @Entity @Table(name="USERS") @Name("user") @Scope(SESSION) public class User implements Serializable { private String m_ID; private String m_Name; private String m_Phone; private String m_Email; private int m_isDeveloper; private int m_isTester; private Integer m_isAdmin; private String m_Password; private String m_TOTRoot; private String m_PWToken; private Integer m_isQPV; public User(String p_id, String p_name, String p_password, String p_email, String p_phone) { super(); m_ID = p_id; m_Name = p_name; m_Password = p_password; m_Email = p_email; m_Phone = p_phone; } public User() { } @Column(name="USR_ID") @Id @NotNull @Length(min=2, max=10) public String getID() { return m_ID; } //Here follow all the needed getters and setters }
The LoginAction:
package myprog.sessions; import java.util.List; import javax.ejb.Interceptor; import javax.ejb.Stateful; import javax.ejb.Stateless; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Out; import org.jboss.seam.contexts.Context; import org.jboss.seam.ejb.SeamInterceptor; import myprog.entities.User; @Stateless @Name("login") @Interceptor(SeamInterceptor.class) public class LoginAction implements ILogin { @In @Out private User user; @PersistenceContext(unitName="myProgDatabase") private EntityManager em; @In private Context sessionContext; @In private FacesContext facesContext; public String login() { List<User> results = em.createQuery( "from User where ID=:username") .setParameter("username", user.getID()).getResultList(); if (results.size() == 0) { facesContext.addMessage(null, new FacesMessage("Invalid login")); return "login"; } else { user = results.get(0); sessionContext.set("loggedIn", true); return "main"; } } }
Here is the component with the called method:
package myprog.sessions; import java.util.List; import javax.ejb.Interceptor; import javax.ejb.Stateful; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.hibernate.validator.Valid; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Out; import org.jboss.seam.annotations.Scope; import org.jboss.seam.contexts.Context; import org.jboss.seam.ejb.SeamInterceptor; import myprog.entities.Testaction; import myprog.entities.User; @Stateless @LoggedIn @Name("showListForDevelopers") @Interceptor(SeamInterceptor.class) public class ShowListForDevelopers implements IShowListForDevelopers { @PersistenceContext(unitName = "myProgDatabase") private EntityManager em; @In @Valid private User user; public Testaction[] getTestactions() { List<Testaction> result = em.createQuery( "from Testaction where DevUsrID=:dev order by ID asc" ).setParameter("dev", user.getID()).getResultList(); return result.toArray(new Testaction[result.size()]); } }
As I said, in getTestactions() the user isn't injected. Has anybody any idea?
Might be it's important to know, that the method is called from a panelNavigation2 item. But I think, this shouldn't make any difference.
Thank you
Newlukai