美好的一天。在我的项目中,我有一个自定义对象列表,我在PrimeFaces p:selectCheckboxMenu
中使用它。我在这门课上使用转换器。下拉列表看起来很棒,如果我从列表中选择一个项目,那么"筹码"正确显示。但是,如果我在初始化时加载所选列表,则选择"芯片"显示类引用(com.test.Characteristic@56e76bff)而不是对象的值。我希望它能从转换器中显示getAsString的返回值。我究竟做错了什么?
转到此代码: 自定义对象
public class Characteristic {
public Characteristic() {
super();
}
private String key;
private String value;
private String type;
public Characteristic(String key, String value, String type) {
this.key = key;
this.value = value;
this.type = type;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
转化器:
@FacesConverter(value = "characteristicConverter")
public class CharacteristicConverter implements Converter {
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
return new Characteristic("testKey", value, "testType");
}
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if (object == null) {
return "";
}
if (object instanceof Characteristic) {
Characteristic cha = (Characteristic) object;
String name = cha.getValue();
System.out.println("In the converter. Returning " + name);
return name;
} else {
throw new ConverterException(new FacesMessage(object + " is not a Characteristic"));
}
}
}
转换器中的System.out.prinln在运行时会显示:
在转换器中。返回char1value
在转换器中。返回char2value
在转换器中。返回char3value
在转换器中。返回char1value
The Bean:
@ManagedBean(name = "checkboxView")
public class CheckboxView {
private List<Characteristic> allChars;
private List<Characteristic> selectedChars;
@PostConstruct
public void init() {
allChars = new ArrayList<Characteristic>();
allChars.add(new Characteristic("char1key","char1value","test"));
allChars.add(new Characteristic("char2key","char2value","test"));
allChars.add(new Characteristic("char3key","char3value","test"));
selectedChars = new ArrayList<Characteristic>();
selectedChars.add(new Characteristic("char1key","char1value","test"));
}
public void setAllChars(List<Characteristic> allChars) {
this.allChars = allChars;
}
public List<Characteristic> getAllChars() {
return allChars;
}
public void setSelectedChars(List<Characteristic> selectedChars) {
this.selectedChars = selectedChars;
}
public List<Characteristic> getSelectedChars() {
return selectedChars;
}
}
页面:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form>
<p:selectCheckboxMenu id="multiple" value="#{checkboxView.selectedChars}" label="Characteristics"
multiple="true" filter="true" filterMatchMode="startsWith"
converter="characteristicConverter">
<f:selectItems value="#{checkboxView.allChars}" var="cha" itemLabel="#{cha.value}" itemValue="#{cha}"/>
</p:selectCheckboxMenu>
</h:form>
</h:body>
</html>
答案 0 :(得分:1)
我怀疑它与将列表中的特征与预先选择的特征进行比较有关(因为在视觉列表中它也没有被选中,并且很可能也会因h:selectManyCheckbox
而失败)。您可以尝试更改
@PostConstruct
public void init() {
allChars = new ArrayList<Characteristic>();
allChars.add(new Characteristic("char1key","char1value","test"));
allChars.add(new Characteristic("char2key","char2value","test"));
allChars.add(new Characteristic("char3key","char3value","test"));
selectedChars = new ArrayList<Characteristic>();
selectedChars.add(new Characteristic("char1key","char1value","test"));
}
到
@PostConstruct
public void init() {
allChars = new ArrayList<Characteristic>();
Characteristic char1 = new Characteristic("char1key","char1value","test");
Characteristic char2 = new Characteristic("char2key","char2value","test");
Characteristic char3 = new Characteristic("char3key","char3value","test");
allChars.add(char1);
allChars.add(char2);
allChars.add(char3);
selectedChars = new ArrayList<Characteristic>();
selectedChars.add(char1);
}
因此,所选的'char1'与列表中的对象明确相同。但更好的方法是尝试明确地实现特征的等于和散列。