View does not communicate with MB

Asked

Viewed 78 times

1

I’m using Primefaces, Javajpa,and Wildfly, there’s not much there but my doubt is the following: my view is not seeing my MB, when I rotate gives the following error:

Target Unreachable, Identifier 'loginMB' resolved to null

I am using the following annotations in my MB:

@Managedbean (name = "loginMB") @Viewscoped

  • Ola Pedro, don’t you use CDI? what import do you use for @Viewscoped?

  • Dilnei I am using CDI, the import I am using is the "import javax.faces.bean.Viewscoped;"

1 answer

0

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.

  • Thanks Dilnei, it worked! I still have some doubts here but this part has already been solved! I’m new to programming,Do you have a method that I write in the field (view) and save in the Database? My fields in BD are already created but how do I type in the view and when click save button save in BD?

  • Good Peter, it depends a lot on the architecture of the software, sometimes I apply certain Patterns, but the basic thing you need to keep in mind is that you should isolate the layers, do not have direct calls from Repositions or DAO’s in managedBean’s have a service layer before, so any business rule you have to have is not in the view bean or in the repository because it only serves to access data,

  • I understand, I’m using everything separately even, it is possible to instruct me in a basic DAO and MB to get the data already registered in the BD and vice versa?

  • relizado update na resposta, abraços.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.