我有以下内容p:pickList
:
<p:pickList id="activityUser" value="#{activityEntryController.activityUserDualListModel}" var="model" itemLabel="#{model.fullName}"
itemValue="#{model}" converter="activityUserListConverter" styleClass="activityUser" required="true" showSourceFilter="true"
showTargetFilter="true" filterMatchMode="contains">
<f:facet name="sourceCaption">Available</f:facet>
<f:facet name="targetCaption">Selected</f:facet>
<p:column style="width: 400px">
<h:outputText value="#{model.fullName}" />
</p:column>
<p:ajax event="transfer" listener="#{activityEntryController.sortUsersOnTransfer}" update="activityUser" />
</p:pickList>
这value
是p:pickList
一个扩展的类DualListModel<T>
:
public class ActivityUserDualListModel extends DualListModel<ActivityUserDTO> {
private static final long serialVersionUID = -5369543532197736480L;
public ActivityUserDualListModel() {
super();
initModel();
}
public ActivityUserDualListModel(List<ActivityUserDTO> source, List<ActivityUserDTO> target) {
super(source, target);
initModel();
}
private void initModel() {
IServiceProvider serviceProvider = (IServiceProvider) ContextLoader.getCurrentWebApplicationContext().getBean(Beans.SERVICE_PROVIDER.getName());
IUserService userService = serviceProvider.getUserService();
List<ActivityUserDTO> activityUserDTOs = new ArrayList<ActivityUserDTO>();
for(User user : userService.getAllUser()) {
if(user.isActive()) {
activityUserDTOs.add(constructActivityUserDTO(user));
}
}
Collections.sort(activityUserDTOs, ComparatorUtils.getActivityUserFullNameComparator());
setSource(activityUserDTOs);
setTarget(new ArrayList<ActivityUserDTO>());
}
private ActivityUserDTO constructActivityUserDTO(User user) {
ActivityUserDTO activityUserDTO = new ActivityUserDTO();
activityUserDTO.setId(user.getId());
activityUserDTO.setUsername(user.getUsername());
activityUserDTO.setFullName(user.getFullName());
return activityUserDTO;
}
}
我是ActivityUserDualListModel
从托管 bean的PostConstruct
方法中实例化的。ActivityEntryController
在事件发生时触发的listener
是:p:ajax
transfer
public void sortUsersOnTransfer(TransferEvent event) {
List<ActivityUserDTO> source = activityUserDualListModel.getSource();
List<ActivityUserDTO> target = activityUserDualListModel.getTarget();
System.out.println("=============================================");
for(ActivityUserDTO dto : source) {
System.out.println(dto.getFullName());
}
System.out.println("=============================================");
System.out.println("=============================================");
for(ActivityUserDTO dto : target) {
System.out.println(dto.getFullName());
}
System.out.println("=============================================");
if(source != null) {
Collections.sort(source, ComparatorUtils.getActivityUserFullNameComparator());
}
if(target != null) {
Collections.sort(target, ComparatorUtils.getActivityUserFullNameComparator());
}
}
奇怪的是target
上面方法中的列表是空的。我无法找到我所缺少的。任何指针都会对我很有帮助。
编辑:
下面的类是转换器:
@FacesConverter("activityUserListConverter")
public class ActivityUserListConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
if (!(component instanceof PickList)) {
return null;
}
if (StringUtils.isBlank(submittedValue)) {
return null;
}
@SuppressWarnings("unchecked")
DualListModel<ActivityUserDTO> pickListModel = (DualListModel<ActivityUserDTO>) ((PickList) component).getValue();
ActivityUserDTO ret = null;
for (ActivityUserDTO activityUserDTO : pickListModel.getSource()) {
if (activityUserDTO.getId().equals(Long.parseLong(submittedValue))) {
ret = activityUserDTO;
break;
}
}
if (ret == null) {
for (ActivityUserDTO activityUserDTO : pickListModel.getTarget()) {
if (activityUserDTO.getId().equals(Long.parseLong(submittedValue))) {
ret = activityUserDTO;
break;
}
}
}
return ret;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
}
if (value instanceof ActivityUserDTO) {
return String.valueOf(((ActivityUserDTO) value).getId());
}
return "";
}
}
我也有以下p:commandButton
提交表格。现在actionListener
没有触发,但是如果我删除p:pickList
了 xhtml 的表单,它就会触发。