SelectOneMenu is not fed

3

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

page.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 in p:selectOneMenu . What do 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 a PostConstruct annotation or in the constructor itself) it does not work.

When I put a "MyManagedBean" object inside the% car_controls manually (by the constructor using ArrayList ), it appears in carros.add(MeuManagedBean) .

    
asked by anonymous 30.11.2014 / 22:11

2 answers

1

MeuManagedBean.java

@ManagedBean(name="meuManagedBean") 

@RequestScoped 
public class MeuManagedBean {

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

       return new CarroDAO().metodoDAO();
}

page.xhtml

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

Notice the name you refer to Bean this menu, not mine.

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

    
02.12.2014 / 02:19
0

I think you should create a Car class with the model and code attributes; Managed Bean manages page requests.
When the page is created it creates an instance of Managed Bean and you are creating multiple MeuManagedBean objects in the AutoDAO and should be the <

So:

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 object conversation itemValue="# {car}" otherwise give error

    
17.01.2015 / 17:41