tags:

views:

85

answers:

1

Can anyone please tell me why the following isn't working?

<h:selectOneMenu value="#{modelosController.selected.idMarca}">
    <br/>
    <f:selectItems value="#{marcasController.itemsAvailableSelectOne}" />
    <br/> 
</h:selectOneMenu><br/>
<h:commandButton action="#{modelosController.createByMarcas}" value="Buscar" />

And the code:

public String createByMarcas() {
          current = new Modelos(selectedItemIndex, current.getIdMarca());
          items =(DataModel)ejbFacade.findByMarcas();
          getPagination().getItemsCount();
          recreateModel();
          return "List";
}

public List<Modelos> findByMarcas(){
    CriteriaQuery cq = (CriteriaQuery) em.createNamedQuery(
             "SELECT m FROM Modelos WHERE m.id_marca :id_marca");
    cq.select(cq.from(Modelos.class));
    return em.createQuery(cq).getResultList();
}

Thank you very much!

+1  A: 

The currently selected item will be set as the value of h:selectOneMenu, in other words, it will be set in #{modelosController.selected.idMarca} but in the action method you're grabbing selectedItemIndex and current.getMarcaId() which doesn't seem to be related to each other.

Here's a basic kickoff example how h:selectOneMenu ought to be used:

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItems value="#{bean.selectItems}" />
</h:selectOneMenu>
<h:commandButton value="submit" action="#{bean.submit}" />

with

private String selectedItem; // +getter +setter
private List<SelectItem> selectItems; // +getter

public Bean() {
    selectItems = new ArrayList<SelectItem>();
    selectItems.add(new SelectItem("value1", "label1"));
    selectItems.add(new SelectItem("value2", "label2"));
    selectItems.add(new SelectItem("value3", "label3"));
    // You can also use SelectItem[] or Map<Object, String> instead.
}

public String submit() {
    // Selected item is already set by JSF. The below line just shows it.
    System.out.println(selectedItem); // value1, value2 or value3
    return null;
}

The selectedItem can also be a Number or any Object. But for the last you'll need a Converter to convert nicely between the Object and a standard type like String or Number.

BalusC
Cheers mate got it working!! muchas gracias
Ignacio
You're welcome :)
BalusC