views:

58

answers:

2

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;
A: 

From the comments, I understand that it's Validation Error: Value not valid.

This basically means that the currently selected item isn't part of the list of selectitems as it is in the current request. It also look like that the item value is a non-standard type (Produtos maybe?). There are three possible causes for this problem:

  1. The equals() and hashCode() of the type representing the item value are not or incorrectly implemented. To fix this, have the IDE autogenerate it or read the javadocs.

  2. A custom converter is been used and the getAsObject() returned the wrong value. To fix this, ensure that it's returning exactly the same value as it was been passed in through getAsString().

  3. The bean is request scoped and the list of selectitems isn't the same as in the initial request when you presented the form. To fix this, you need to ensure that you preserve the same list in the subsequent request. If you're already on JSF 2.0, declaring the bean @ViewScoped would fix it. If you're on JSF 1.x, then you need either put the bean in session scope or to do the list loading in the bean constructor.

BalusC
I posted in a follow up question what I found
Ignacio
BalusC I know I'm asking too much but is my first web app and I really need your help (I had it working but last changes ruined everything and I don't know why)
Ignacio
Sorry, non-English and non-intuitive code in non-SSCCE format makes it hard to understand what's going on. I've at least pointed the possible causes. It's up to you to debug and investigate them. Maybe [this article](http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html) is more useful. Don't modify your code yet, just start clean to get a understanding and then build on that further.
BalusC
Balus I can debbug cause while starting glashfish the following is thrown:WEB0517 Unable to restore sessions for web module Plugin insertion failed: Could not find plugin from previous deployment CauseAn HTTP session that was supposed to survive the redeployment of the web module failed to be restored (deserialized). This can happen if an application class that was used as a session attribute has changed incompatibly. I commmented (//) all changes and still exception is thrwon while loading, what can I do?
Ignacio
never mind just solved this issue! will keep on working
Ignacio
A: 

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");?

Ignacio