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

五迷三道 提交于 2019-12-24 12:18:47

问题


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;

回答1:


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.




回答2:


Thanks too much balusc, I had the same problem and I've solved it.

  1. 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.



回答3:


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



来源:https://stackoverflow.com/questions/3039338/jsf-validation-error-shown-by-hmessage-while-updating-model-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!