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)
.