I have a web app that uses JSF pages and entity classes, where a user has to authenticate to be able to publish some exercises to his students.
In the entity class, a "user" object has: id(int), login(string), password(string) and type(string)
In my "users" controller bean, i have a method to see if the user already exists in database. And that process goes well. All is working fine. So, i know for sure that a "user" object is instantiated with the correct data.
I have another controller that is used in another JSF page where the user can publish an exercise.
In the entity class, an "exercise" object has: id(int), userID(int)(foreign key to ID on users), description(string), etc.
When i'm preparing the "exercise" object in the "exercises" controller, to save to database, i need to have access to the "user" object in the other controller to set the userID. And this is the reason of my headache! Because the userID allways sets to null !!!
I've tryed with @Named and @Inject(recommended), also with @Managedbean and @ManagedProperty(wich they say will be deprecated) ... Tryed with single and mixed combinations of @RequestScoped and @SessionScoped ... Tryed to instantiated the "user" object inside a @PostConstruct method in "exercises" controller... Have already read many examples on how to do it, but none of it works!
I know this is a very basic problem i'm having! And it only consists on how to pass an object from one controller to another! Still no SUCCESS, after TWO days of experimenting! This is driving me crazy!
Please HELP!:
"users" Controller
package controlers;
import beans.UtilizadoresFacade;
import entities.Utilizadores;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named("utilizadoresController")
@RequestScoped
public class UtilizadoresController implements Serializable {
@EJB
UtilizadoresFacade ejbFacade;
Utilizadores utilizador = new Utilizadores(); //The one i want to pass to the other controller
List<Utilizadores> utilizadores = new ArrayList();
public Utilizadores getUtilizador() {
return utilizador;
}
public void setUtilizador(Utilizadores utilizador) {
this.utilizador = utilizador;
}
public List<Utilizadores> getUtilizadores() {
return utilizadores = (List<Utilizadores>)ejbFacade.consultarUtilizadores();
}
public String verificarUtilizador() { // method to verify the user in database by name and password
utilizadores = (List<Utilizadores>)ejbFacade.consultarUtilizador(utilizador.getNome(), utilizador.getSenha());
if(utilizadores.isEmpty())
return "index.xhtml";
else if(utilizadores.get(0).getTipo().equals("normal")) {
utilizador.setId(utilizadores.get(0).getId()); // Here is where i save the ID to "user" object
return "normal.xhtml";
}
else {
utilizador.setId(utilizadores.get(0).getId());
return "administrador.xhtml";
}
}
public String criarUtilizador(){ // this method is to add new user to database and it works fine.
ejbFacade.adicionarUtilizador(utilizador);
if(utilizador.getTipo().equals("normal"))
return "normal.xhtml";
else
return "administrador.xhtml";
}
}
"exercises" controller
package controlers;
import beans.ExerciciosFacade;
import entities.Exercicios;
import entities.Ucs;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named("exerciciosController")
@RequestScoped
public class ExerciciosController implements Serializable {
@EJB
ExerciciosFacade ejbFacade;
@Inject
UtilizadoresController utilizadoresController;
Ucs cadeira = new Ucs(); //this is a discipline
Exercicios exercicio = new Exercicios();
List<Exercicios> exercicios = new ArrayList();
public List<Exercicios> getExercicios(){
return exercicios = (List<Exercicios>)ejbFacade.consultarExercicios();
}
public String criarExercicio(){ // Method to add an exercise to database
exercicio.setUcid(cadeira); // Sets de discipline ID to "exercise" object (another foreign key) and works well because this value comes from a form in JSF page
exercicio.setUtilizadorid(utilizadoresController.getUtilizador()); // This is where i try to set de user ID to "exercise" object
ejbFacade.adicionarExercicio(exercicio);
return "normal.xhtml";
}
public Exercicios getExercicio() {
return exercicio;
}
public void setExercicio(Exercicios exercicio) {
this.exercicio = exercicio;
}
public Ucs getCadeira() {
return cadeira;
}
public void setCadeira(Ucs cadeira) {
this.cadeira = cadeira;
}
}
I think the "users" controller should be @SessionScoped because i'll be needing the user data while he is navigating through the pages, but i've used @RequestScoped also so there won't be more conflicts i can't control !!!
One more thing: I'm working with Netbeans 8.1 and java DB also from Netbeans.
Hope i'm not forgetting anything!
Many thanks in advance.