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()
– NilsonUehara
http://stackoverflow.com/questions/6848970/how-to-prepopulate-a-hselectonemenu-with-complex-objects-entities-from-a-db
– João Manolo
@Nilsonuehara, he’s there, just hadn’t put.
– ptkato
@Jonathan, then the method with
@PostConstruct
must be theinit()
? It can’t be theattCarros()
?– ptkato
@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
@utluiz It does not appear in the list, because the other methods of the same class work.
– ptkato