更新模型时,Jsf验证错误(由h:message显示),为什么?

时间:2010-06-14 17:20:12

标签: jsf ejb eclipselink

List.xhtml:

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

控制器类方法:

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

Facade Class方法:

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

Bean类查询:

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

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

3 个答案:

答案 0 :(得分:2)

从评论中我知道它是Validation Error: Value not valid

这基本上意味着当前所选项目不是当前请求中的选择项列表的一部分。看起来项目值也是非标准类型(Produtos可能?)。导致此问题的原因有三种:

  1. 表示项目值的equals()hashCode()未实现或未正确实施。要解决此问题,请让IDE自动生成它或阅读javadocs

  2. 使用了自定义转换器,getAsObject()返回了错误的值。要解决此问题,请确保它返回的与通过getAsString()传递的完全相同的值。

  3. bean是请求作用域,当您提交表单时,选择项列表与初始请求中的列表不同。要解决此问题,您需要确保在后续请求中保留相同的列表。如果您已经使用JSF 2.0,则声明bean @ViewScoped会修复它。如果你使用的是JSF 1.x,那么你需要将bean放在会话范围内或者在bean构造函数中加载列表。

答案 1 :(得分:0)

非常感谢balusc,我遇到了同样的问题,我已经解决了。

  1. bean是请求作用域,并且当您提交表单时,选择项列表与初始请求中的相同。要解决此问题,您需要确保在后续请求中保留相同的列表。如果您已经使用JSF 2.0,则声明bean @ViewScoped会修复它。

答案 2 :(得分:-1)

这是一个:

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

这是另一个:

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

这是否导致velue丢失:getValue(facesContext.getELContext()null,“produtosController”);?