如何将属性传递给复合组件

时间:2012-02-05 18:59:09

标签: jsf-2 composite-component

我在以正确的方式使用JSF复合组件时遇到了麻烦。我把一些组件放在一起,一切正常。然后我只是将代码提取到复合​​组件中,并传递了相应的属性,突然间我得到了对话异常。

<composite:interface>
    <composite:attribute name="selectedIDs"  type="java.util.Collection" required="true"/>
    <composite:attribute name="selectItems" type="java.util.List" required="true" />    
    <composite:attribute name="addAction" required="true"/>
    <composite:attribute name="deleteAction" required="true"/>
    <composite:attribute name="deleteButtonDisabled" />
    <composite:attribute name="ajaxListener" method-signature="void listener(javax.faces.event.AjaxBehaviorEvent)"/>
</composite:interface>
<composite:implementation>
<div class="myClass">
    <h:outputStylesheet library="views" name="selectManyControlPanel.css" target="head" />

    <h:form>
        <h:selectManyListbox value="#{cc.attrs.selectedIDs}">
            <f:selectItems value="#{cc.attrs.selectItems}" />
            <f:ajax render="delete"
                listener="#{cc.attrs.ajaxListener}" />
        </h:selectManyListbox>
        <br />
        <h:commandButton id="delete" value="Delete"
            disabled="#{cc.attrs.deleteButtonDisabled}"
            action="#{cc.attrs.deleteAction}" />
        <h:commandButton id="add" value="Add" action="#{cc.attrs.addAction}"/>
    </h:form>
</div>
</composite:implementation>

这是我使用新创建的组件的页面

<view:selectManyControlPanel 
            selectedIDs="#{bean.selectedIds}" 
            selectItems="#{bean.listOfSelectItems}"
            addAction="#{bean.addNew}"
            deleteAction="#{bean.deleteSelection}"
            ajaxListener="#{bean.selectionChanged}"
            deleteButtonDisabled="#{bean.deleteButtonDisabled}" />

Bean(某些方法跳过了重命名的部分)

package views;

@SuppressWarnings("serial")
@Named
@RequestScoped
public class Bean implements Serializable, IOverviewView {

    @Inject
    Presenter presenter;
    private boolean deleteButtonDisabled;

    private List<SelectItem> listOfSelectItems;

    private Set<Long> selectedIds;


    public Bean(){
        deleteButtonDisabled = true;
        listOfSelectItems = new ArrayList<SelectItem>(10);
    }


    public void selectionChanged(AjaxBehaviorEvent event){
        if(selectedIds.isEmpty())
            deleteButtonDisabled = true;
        else
            deleteButtonDisabled = false;
    }

    public void deleteBikes(){
        presenter.delete(selectedIds);
    }

    public void addNew(){
        presenter.addNew();
    }

    public List<SelectItem> getListOfSelectItems() {
        return listOfSelectItems;
    }

    public Set<Long> getSelectedIds() {
        return selectedIds;
    }           

    @PostConstruct
    public void init(){
        System.out.println("INITIALIZING BEAN: " + this.getClass().getName());
        this.presenter.setView(this);
        this.presenter.init();
    }

    public boolean isDeleteButtonDisabled() {
        return deleteButtonDisabled;
    }

    @Override
    public void setDeleteButtonEnabled(boolean isEnabled) {
        deleteButtonDisabled = !isEnabled;
    }

    public void setListOfSelectItems(List<SelectItem> list) {
        this.listOfSelectItems = list;
    }

    public void setSelectedIds(Set<Long> selectedIds) {
        this.selectedIds = selectedIds;
    }


    @Override
    public void updateBikesList(Set<ViewObject> objectsToDisplay) {
        updateList(objectsToDisplay);
    }


    private void updateList(Set<ViewObject> objectsToDisplay){
        listOfSelectItems.clear();
        for (ViewObject vO : objectsToDisplay) {
            final String label = vO.getManufacturer() + ", " + vO.getModel() + " (" + vO.getYear() + ")";
            listOfSelectItems.add(new SelectItem(vO.getId(), label));
        }
    }
....

}

异常

javax.el.ELException: /resources/views/selectManyControlPanel.xhtml @25,56 value="#{cc.attrs.selectedIDs}": /index.xhtml @21,70 selectedIDs="#{bean.selectedIds}": Cannot convert [Ljava.lang.String;@1e92093 of type class [Ljava.lang.String; to interface java.util.Set

唯一改变的是我使用的是组合而不是普通代码。 EL表达式仍然相同。有人可以开导我吗?我希望参数只是通过,但显然不是......

1 个答案:

答案 0 :(得分:12)

这与Mojarra issue 2047有关。它计划在即将到来的2.2中修复。

问题单还提出了以下解决方法:

<view:selectManyControlPanel 
    selectedIDsBean="#{bean}" 
    selectedIDsProperty="selectedIds" 

with CC interface

<composite:attribute name="selectedIDsBean" required="true"/>
<composite:attribute name="selectedIDsProperty" required="true"/>

并在CC实施中

<h:selectManyListbox value="#{cc.attrs.selectedIDsBean[cc.attrs.selectedIDsProperty]}">