伙计们,我正在尝试遍历用户定义对象的列表,但出现此错误(java.lang.String无法转换为bg.fmi.master.thesis.model.TFilterType),我不知道为什么。
我的.xhtml我有:
<p:selectManyCheckbox id="chkbox1"
value="#{requestBean.selectedBooleanFilterTypes}"
layout="pageDirection">
<f:selectItems var="checkbox"
value="#{filterTypeBean.listBooleanFilterTypes()}"
itemLabel="#{checkbox.filterTypeName}" itemValue="#{checkbox}" />
<!-- required="true"
requiredMessage="check at least one checkbox" -->
</p:selectManyCheckbox>
来自bean类的一部分:
private List<TFilterType> selectedBooleanFilterTypes;
public List<TFilterType> getSelectedBooleanFilterTypes() {
return selectedBooleanFilterTypes;
}
public void setSelectedBooleanFilterTypes(
List<TFilterType> selectedBooleanFilterTypes) {
this.selectedBooleanFilterTypes = selectedBooleanFilterTypes;
}
这是另一个方法的一部分,但在bean类中也是如此:
for (TFilterType type : selectedBooleanFilterTypes) {
System.out.println("SelectedFilterTypes: "
+ type.getFilterTypeName());
}
在调试模式下,我可以看到selectedBooleanFilterTypes具有以下值:
[TFilterType [filterTypeName = DJ,filterTypeDesc = DJ,isBooleanType = B,tRequestFilters = []],TFilterType [filterTypeName =Украса,filterTypeDesc = Decoration,isBooleanType = B,tRequestFilters = []]
提前致谢!
最佳答案
TFilterType是一个Java类。在这种情况下,您应该为您的类型使用Faces Converter。请尝试以下示例
xhtml:
<p:selectManyCheckbox id="chkbox1" value="#{requestBean.selectedBooleanFilterTypes}"
layout="pageDirection" converter="filterTypeConverter">
<f:selectItems var="checkbox" value="#{filterTypeBean.listBooleanFilterTypes()}"
itemLabel="#{checkbox.filterTypeName}" itemValue="#{checkbox}"/>
</p:selectManyCheckbox>
转换器:
@FacesConverter("filterTypeConverter")
public class TFilterTypeConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
FilterTypeBean filterTypeBean = context.getApplication().evaluateExpressionGet(context, "#{filterTypeBean}", FilterTypeBean.class);
for (TFilterType type : filterTypeBean.listBooleanFilterTypes()) {
if (type.getFilterTypeName().equals(value)) {
return type;
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value instanceof TFilterType) {
return ((TFilterType) value).getFilterTypeName();
} else {
return "";
}
}
}