0

我正在处理选项列表,这是我的代码:

xhtml代码:

    <p:pickList id="pickList" value="#{projectAdminisrationMB.selectedUserGroups}" 
          var="group"   
            itemLabel="#{group}" itemValue="#{group.name}" 
            converter="groupConverter"
            > 
             <f:facet name="sourceCaption">Belong To</f:facet>  
             <f:facet name="targetCaption">Exclude From</f:facet> 

              <p:column style="width:25%">  
                    <p:graphicImage value="/images/group/#{group.name}.gif" />  
               </p:column>  

             <p:column style="width:75%;">  
                  #{group.name}  
             </p:column> 
              <p:ajax event="transfer" listener="#
          {projectAdminisrationMB.onTransferGroup}" update="growl" />  
             </p:pickList> 

我的转换器:

package tn.talan.testFramework.converter;


 @FacesConverter(value = "groupConverter")
  public class GroupConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {


    return administrationProjectFinal.getGroupByName(value);

     Object ret = null;
        if (component instanceof PickList) {
            Object dualList = ((PickList) component).getValue();
            DualListModel dl = (DualListModel) dualList;
            for (Object o : dl.getSource()) {
                String id = "" + ((Group) o).getIdBdd();
                if (value.equals(id)) {
                    ret = o;
                    break;
                }
            }
            if (ret == null)
                for (Object o : dl.getTarget()) {
                    String id = "" + ((Project) o).getIdBdd();
                    if (value.equals(id)) {
                        ret = o;
                        break;
                    }
                }
        }
        return ret;

}

@Override
public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    System.out.println("converter group getAsString  " + value);

     String str = "";
        if (value instanceof Group) {
            str = "" + ((Project) value).getIdBdd();
        }
        return str;


}
  }

我的目标是从选择列表中检索添加或删除的项目,所以这里是 OnTransferGroup 方法:

 public void  onTransferGroup(TransferEvent event) { 

     if(event.isAdd()){

         String resMsg;
            System.out.println("dropped group to be excluded");

             for(Object item : event.getItems()) { 

                    Group selectedBelongToGroup =(Group) item;




    resMsg=administrationProjectFinal.exclureUserGroupFn(selectedBelongToGroup 
     ,selectedUserRow);

                    if (resMsg.equals("The project should have at least 
                               one administrator"))
                    {setDeleteGroupFlag(true);
                     showErrorMsg(resMsg);
                    }

                   else droppedBelongToGroups.add(selectedBelongToGroup); 

                }
         }


     else if(event.isRemove()){
         System.out.println("remove");
         System.out.println("dropped group to be included");
         for(Object item : event.getItems()) {  

        Group selectedExcludeFromGroup = ((Group)item); 
        System.err.println(" n" +selectedExcludeFromGroup.getName());
        System.err.println("d "+selectedExcludeFromGroup.getDescription());



       droppedExcludeFromToGroups.add(selectedExcludeFromGroup); 

        administrationProjectFinal.inclureUserGroup(selectedExcludeFromGroup 
        ,selectedUserRow);

         }



     }                   

         }  

问题是 event.getItems() 列表总是空的。当我第一次选择项目时,onTransferGroup(TransferEvent 事件) 并没有被午餐,但在那之后它是 luched。

那么如何检索要添加或删除的选定项目

4

1 回答 1

2

您在“getAsObject”方法的第一条语句上放了一个“return”,这样它就不会到达逻辑的其余部分。
此外,如果您从 getItems 方法中得到一个空返回值,这肯定意味着转换器问题,因此请检查上述内容。

于 2012-11-09T17:01:53.617 回答