1

我有一个简单的复合组件,它必须渲染 h:selectManyListbox 和与之关联的 h:messages。问题在于使用面孔转换器类。它似乎无法使用它(仅在组件代码中使用时)。

复合组件代码:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:composite="http://java.sun.com/jsf/composite">

<!-- INTERFACE -->
<composite:interface>
    <composite:attribute name="description" required="true" />
    <composite:attribute name="fieldValue" required="true" />
    <composite:attribute name="idc" required="true" />
    <composite:attribute name="size" required="true" />
</composite:interface>

<!-- IMPLEMENTATION -->
<composite:implementation>
    <tr>
        <td nowrap="nowrap">
           <h:outputText value="#{cc.attrs.description}" />
        </td>
        <td>
           <h:selectManyListbox 
                id="#{cc.attrs.idc}" 
                size="#{cc.attrs.size}"
                value="#{cc.attrs.fieldValue}">                 
            <composite:insertChildren />
           </h:selectManyListbox>
        </td>
        <td>
           <h:message for="#{cc.attrs.idc}" />
        </td>
    </tr>
</composite:implementation>
</html>

当我在 sample.xhtml 页面上使用它时(如下所示),我得到“验证错误:值无效”。......

但是当我在同一页面上输入如下代码时:

<tr>
    <td><h:outputText value="Plugins" /></td>
    <td>
         <h:selectManyListbox 
            id="plugins"
            value="#{bean.currentPlugins}" 
                   size="6">
        <f:selectItems value="#{bean.availablePlugins}" />
        </h:selectManyListbox>
    </td>
    <td><h:message for="plugins" /></td>
</tr>

一切顺利。

托管 bean 'bean' 类是

@ManagedBean
@SessionScoped
public class Bean extends GenericManagedBean
        implements Serializable {
   ElementClass[] currentPlugins;

    // getter & setter for currentPlugins
    // ...

   public List<ElementClass> getAvailablePlugins() {
       // ..  some code
       return list;
   }
}

和 ElementClass 是

public class ElementClass extends GenericEntity implements Serializable {

    private static final long serialVersionUID = 9159873495276902436L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    // other fields
    // ...

    // getters & setters
    // ...

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof ElementClass)) {
            return false;
        }
        ElementClass other = (ElementClass) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
}

对于 ElementClass 我有转换器类

@FacesConverter(forClass = ElementClass.class)
public class ElementClassConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uiComponent,
            String elemId) {

        if (!StringUtils.isEmpty(elemId)) {
            // this code gets ElementClass object entity from database
            ApplicationBean applicationBean = JSFHelper
                    .findBean("applicationBean");
            return applicationBean.getService()
                    .findElementClassById(
                            Integer.parseInt(elemId));
        } else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uiComponent,
            Object elem) {
        return elem != null ? ((ElementClass ) elem).getId()
                .toString() : "";
    }
}

和标准问题:我做错了什么?我认为这是我缺乏知识而不是 jsf 实现错误。我使用 JBoss 7.1.1 AS 和所有东西,它捆绑了什么 + RichFaces 4。

4

0 回答 0