selectOneMenu中的EJB更新模型

时间:2010-06-07 04:57:36

标签: java jsf

任何人都可以告诉我为什么以下不起作用?

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

代码:

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();
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

当前选中的项目将设置为h:selectOneMenu,换句话说,它将在#{modelosController.selected.idMarca}中设置,但在您抓取的操作方法中selectedItemIndexcurrent.getMarcaId()似乎彼此无关。

以下是应该使用h:selectOneMenu的基本启动示例:

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

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;
}

selectedItem也可以是Number或任何Object。但是对于最后一个,您需要ConverterObjectStringNumber等标准类型之间进行良好的转换。