0

我有一个搜索系统的搜索功能。执行搜索后,我正在更新搜索结果以选择列表。每次我搜索我都会更新pickList源。现在我的问题是我想以这样一种方式进行验证,即如果将超过 1 个值添加到选择列表目标它应该给我错误消息并检查选择列表目标中的重复条目。

下面是 xhmtl 中选择列表的代码

    <p:pickList id="primaryContactBean" value="#{addSystemManagedBean.primaryContactList}" var="contact" itemLabel="#{contact.firstName}" itemValue="#{contact}" converter="contactConverter" >
   <p:ajax event="transfer" listener="#{addSystemManagedBean.onTransfer}" process="@this"          update="sysMsg3" />
  <p:column>
     #{contact.firstName} #{contact.lastName}
   </p:column>
</p:pickList>

并 onTransfer 调用以下方法

public int primaryOwnerCount = 0;
    public void onTransfer(TransferEvent event) {
        PickList picklist = (PickList) event.getComponent();
            for(Object item : event.getItems()) {  
                ContactBean cb = (ContactBean)item;
                if(picklist.getId().equals("primaryContactBean")){
                    if(event.isAdd())
                    primaryOwnerCount++;
                    if(primaryOwnerCount>1){
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Only one primary owner can exist!!","If you want to add owner, please remove from the list and then proceed.."));
                        for (Iterator iterator = primaryContactList.getTarget().iterator(); iterator
                                .hasNext();) {
                            ContactBean type = (ContactBean) iterator.next();
                            System.out.println("target:"+type.getFirstName());
                        }


                    }

                    else
                        primaryContactTarget.add(cb);
                }
4

0 回答 0