我有selectManyCheckbox,但在更改事件后我无法从中获取选中的值(所选值的列表保持为空)。 触发更改事件,但在调试模式下,listSelectedSignalCauses中没有值。 我试图设置一些转换器,但它也没有帮助(也许它是错误的转换器)。
<p:selectManyCheckbox value="#{SignalAnalysisBean.listSelectedSignalCauses}" layout="pageDirection" styleClass="lstSignal">
<f:selectItems value="#{SignalAnalysisBean.listSignalCauses}" var="causes" itemValue="#{causes}" itemLabel="#{causes.name}" />
<p:ajax process="@this" update="test" event="change" listener="#{SignalAnalysisBean.checkCausesList}" />
</p:selectManyCheckbox>
<h:outputText value="#{SignalAnalysisBean.arrSize}" id="test" />
public class SignalAnalysisBean extends AbstractBean {
private List<GeneralCollDTO> listSignalCauses;
private List<GeneralCollDTO> listSelectedSignalCauses;
//...
public void checkCausesList(){
arrSize = 4 + listSelectedSignalCauses.size();
}
//...
}
public class GeneralCollDTO {
private String name;
private String value;
public GeneralCollDTO() { }
public GeneralCollDTO(String n, String v) {
this.name = n;
this.value = v;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
答案 0 :(得分:1)
要将自定义对象发送到托管bean,您需要实现转换器,否则您将无法执行此操作。下面的代码基于您的代码实现它,它可能是解决问题的方法。
@FacesConverter(value = "generalCollDTOConverter", forClass = GeneralCollDTOConverter.class)
public class GeneralCollDTOConverter implements Converter {
@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
if (value != null) {
return component.getAttributes().get(value);
}
return null;
}
@Override
public String getAsString(FacesContext ctx, UIComponent component, Object value) {
if (value != null && value instanceof GeneralCollDTO) {
GeneralCollDTO generalCollDTO = (GeneralCollDTO) value;
if (generalCollDTO.getName() != null) {
component.getAttributes().put(generalCollDTO.getName(), value);
return generalCollDTO.getName();
}
}
return null;
}
}
<p:selectManyCheckbox converter="generalCollDTOConverter" value="#{SignalAnalysisBean.listSelectedSignalCauses}" layout="pageDirection" styleClass="lstSignal">
答案 1 :(得分:1)
最终我最终得到了适用于我的通用转换器。
@FacesConverter(value = "generalCollDTOConverter", forClass = GeneralCollDTOConverter.class)
public class GeneralCollDTOConverter implements Converter {
private static Map<Object, String> entities = new HashMap<Object, String>();
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
synchronized (entities) {
if (!entities.containsKey(value)) {
String uuid = UUID.randomUUID().toString();
entities.put(value, uuid);
return uuid;
} else {
return entities.get(value);
}
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
for (Entry<Object, String> entry : entities.entrySet()) {
if (entry.getValue().equals(value)) {
System.out.println("getKey:" + entry.getKey());
return entry.getKey();
}
}
return value;
}
}