If you use CDI you should use named which is a Qualifier, basically what it does is make your bean accessible through EL.
The import for the scoped view should be faces.view.Viewscoped and not faces.bean.Vie wScoped
Behold doc
import javax.inject.Named;
import javax.faces.view.ViewScoped;
@Named(value = "loginMB")
@ViewScoped
public class LoginMB implements Serializable {
// cod...
}
@Managedbean is JSF already this deprecated.
Ex: from communication to database
@Named(value = "loginMB")
@ViewScoped
public class LoginMB implements Serializable {
@Inject
SeuServico servico
@Transactional
public void save() {
servico.salvar(algumObjeto);
}
}
Service:
public class SeuServico implements Serializable {
@Inject
SeuRepository repository;
public AlgumObjeto salvar(AlgumObjeto AlgumObjeto) {
return repository.persiste(user);
}
}
Repository:
@Repository
public class SeuRepository implements Serializable {
private final EntityManager em;
@Inject
public SeuRepository(EntityManager em) {
this.em = em;
}
public AlgumObjeto persiste(AlgumObjeto algumObjeto) {
AlgumObjeto = em.merge(algumObjeto);
em.flush();
return AlgumObjeto;
}
}
Note: the Repository annotation you will not find in java, that’s because I create it, I say that my repositories are dependent on CDI, no link has she
If you ask yourself: Why have a service layer if they do what my repositories do? is what I said above, if they are basic things like saving, findById of course it has no logic, but if you need to validate if you do some services with not elegant codes, it is in the service that the dirty work should be carried out.
You can still consider using generic repositories, I do not like, I do not like heritage in the repostirórios, but it is my thing, sequiser, use but it already goes beyond the scope of the question, hugs.
Ola Pedro, don’t you use CDI? what import do you use for @Viewscoped?
– Dilnei Cunha
Dilnei I am using CDI, the import I am using is the "import javax.faces.bean.Viewscoped;"
– Pedro