我正在制作选项列表,这是我的代码:
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事件)没有被发送,但是在此之后它被浏览了。
那么如何检索要添加或删除的选定项目
答案 0 :(得分:2)
您在“getAsObject”方法的第一个语句中放置了“return”,因此它不会到达逻辑的其余部分。
此外,如果您从getItems方法中获得空返回值,则肯定意味着转换器问题,因此请检查以上内容。