Search code examples
jsfejbeclipselink

Jsf validation error (shown by h:message) while updating Model, why?


List.xhtml:

 <h:selectOneMenu value="#{produtosController.selected.codigo}">
    <f:selectItems value="#{produtosController.itemsAvailableSelectOne}"/>
 </h:selectOneMenu>
 <h:commandButton action="#{produtosController.createByCodigos}" value="Buscar" />  

Controller Class method:

 public String createByCodigos(){
    items = new ListDataModel(ejbFacade.findByCodigos(current.getCodigo()));
    updateCurrentItem();
    return "List";
}  

Facade Class method:

 public List<Produtos> findByCodigos(Integer codigo){
    Query q = em.createNamedQuery("Produtos.findByCodigo");
    q.setParameter("codigo", codigo);
    return q.getResultList();
}

Bean Class query:

 @NamedQuery(name = "Produtos.findByCodigo", query = "SELECT p FROM Produtos p WHERE p.codigo = :codigo")

 @Column(name = "codigo")
 private Integer codigo;

Solution

  • Here is one:

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Produtos) {
                Produtos o = (Produtos) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+ProdutosController.class.getName());
            }
    

    Here is the other:

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ProdutosController controller = (ProdutosController)facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "produtosController");
            return controller.ejbFacade.find(getKey(value));
        }
    

    Is this where the velue gets lost: getValue(facesContext.getELContext() null, "produtosController");?