Selectonemenu is not powered

Asked

Viewed 373 times

3

I have a p:selectOneMenu and would like it to be fed with existing data in the database as soon as the page was loaded:

xhtml.

<p:selectOneMenu id="carros">
    <f:selectItem itemLabel="Carros" itemValue="0"/>
    <f:selectItems value="#{menuManagedBean.carros}" var="carro" itemLabel="#{carro.modelo}" itemValue="#{carro}"/>
</p:selectOneMenu>

Meumanagedbean.java

@ManagedBean
@RequestScoped
public class MeuManagedBean {

    private String modelo;
    private int codigo;
    private ArrayList<MeuManagedBean> carros;
    //getter-setter

    @PostConstruct
    public void attCarros() {
        carros = new CarroDAO.metodoDAO();
    }
}

Carrodao.java

public class CarroDAO {
    //conexao...
    public ArrayList<MeuManagedBean> metodoDAO() {
        ResultSet rs //...
        ArrayList<MeuManagedBean> carros = new ArrayList<MeuManagedBean>();
        while (rs.next()) {
            MeuManagedBean carro = new MeuManagedBean();
            carro.setCodigo(rs.getInt("cdCarro"));
            carro.setModelo(rs.getString("nmModelo"));
            carros.add(carro);
        }
    }
    //...
    return carros;
}

When accessing the page nothing appears on p:selectOneMenu. What I have to fix to make it work?


I noticed a very strange behavior. Every time I try to add a method that is immediately called on the page (i.e. with an annotation PostConstruct or in the constructor itself) it does not work.

When I place a "Meumanagedbean" object inside the ArrayList cars manually (by constructor using carros.add(MeuManagedBean)), he appears in the p:selectOneMenu.

  • Meumanagedbean needs to have a getCarros()

  • http://stackoverflow.com/questions/6848970/how-to-prepopulate-a-hselectonemenu-with-complex-objects-entities-from-a-db

  • @Nilsonuehara, he’s there, just hadn’t put.

  • @Jonathan, then the method with @PostConstruct must be the init()? It can’t be the attCarros()?

  • @Patrick When you did that @PostConstruct does not work, what exactly occurs? It does not run or the recovered value does not appear in the list? If you haven’t checked this, see if you’re at least passing that line.

  • @utluiz It does not appear in the list, because the other methods of the same class work.

Show 1 more comment

2 answers

1

Meumanagedbean.java

@ManagedBean(name="meuManagedBean") 

@RequestScoped 
public class MeuManagedBean {

//code...
public List< MeuManagedBean > getCarros(){

       return new CarroDAO().metodoDAO();
}

xhtml.

<f:selectItems value="#{menuManagedBean.carros}".../>

Note the name that you refer to the bean in this menu and not mine.

Tip: Rename methods according to their function, swap medotoDAO for getCarros or listarCarros.

  • It seems you are confused as to the formatting of questions/answers/comments. From a look in this article. Try to put explanations next to the answers, besides being easy to understand, the probability of gaining positive reputations and a marking as an accepted answer also increase.

  • I have a problem answering some excerpts were not coming out correctly and did not appear. I am editing slowly. If Voce can edit and check if the problem happens with vc tbm, the line where it is < f:select cannot if I join the character < the line

  • Suggested the second option, for the method name: listarCarros(). The getCarros() gives me the impression that you return a class attribute.

0

I think you should create a Car class with template and code attributes; Managed Bean manages page requests.
When the page is created it creates an instance of Managed Bean and vc is creating several Meumanagedbean objects in Carrodao and should be the Car object

Thus:

while (rs.next()) {
    Carro carro = new Carro();
    carro.setCodigo(rs.getInt("cdCarro"));
    carro.setModelo(rs.getString("nmModelo"));
    carros.add(carro);
}

And create a convert to itemValue object conversation="#{car}" otherwise gives error

Browser other questions tagged

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