Im testing a little aplication in Java EE 6 with EJB 3.1 and JSF 2.0.
The question is that, when I call the Backing bean in JSF with a EL expression, and run the application, I get Exception because this backing bean is inaccesible and is null.
Here is my JSF code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:head>
<title>Insertar Usuarios</title>
</h:head>
<h:body>
<h:form id="registro">
<h:panelGrid columns="2">
<h:outputText value="Username:"/>
<h:inputText id="username" value="#{registro.usuario.user_Name}"/>
<h:outputText value="Id:"/>
<h:inputText id="id_user" value="#{registro.usuario.userId}"/>
<h:outputText value="Mensaje:"/>
<h:inputText id="mensaje" value="#{registro.usuario.userMessage}" required="true"/>
<h:commandButton value="Crear usuario" action="#{registro.insertarUsuario()}" />
</h:panelGrid>
<h:messages/>
</h:form>
</h:body>
</html>
Here is my Stateless EJB:
@Stateless
public class NegocioImpl implements Negocio{
@PersistenceContext(unitName = "manager1")
private EntityManager em;
@Override
public void sayHelloFromServiceBean(User user) {
User u = em.find(User.class, user.getUserId());
if(u != null){
System.out.println("Cuenta existente");
}
em.persist(user);
}
public String hola(){
return "hola";
}
}
And finally the backing bean:
@Named
@RequestScoped
public class Registro {
@Inject
private Negocio negocio;
private User usuario;
public Registro(){
usuario = new User();
}
public User getUsuario() {
return usuario;
}
public void setUsuario(User usuario) {
this.usuario = usuario;
}
public void insertarUsuario(){
negocio.sayHelloFromServiceBean(usuario);
}
public void saluda(){
negocio.hola();
}
}
The project structure is make with Intellij IDEA.
Thanks in advance