Reputation: 207
guys, i am trying to iterate though a list of user defined objects but i get this error (java.lang.String cannot be cast to bg.fmi.master.thesis.model.TFilterType) and i can not figure out why.
I my .xhtml i have:
<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>
Part from the bean class:
private List<TFilterType> selectedBooleanFilterTypes;
public List<TFilterType> getSelectedBooleanFilterTypes() {
return selectedBooleanFilterTypes;
}
public void setSelectedBooleanFilterTypes(
List<TFilterType> selectedBooleanFilterTypes) {
this.selectedBooleanFilterTypes = selectedBooleanFilterTypes;
}
This is part from another method, but also in the bean class:
for (TFilterType type : selectedBooleanFilterTypes) {
System.out.println("SelectedFilterTypes: "
+ type.getFilterTypeName());
}
During Debugging mode i can see that selectedBooleanFilterTypes has this value:
[TFilterType [filterTypeName=DJ, filterTypeDesc=DJ, isBooleanType=B, tRequestFilters=[]], TFilterType [filterTypeName=Украса, filterTypeDesc=Decoration, isBooleanType=B, tRequestFilters=[]]]
Thanks in advance!
Upvotes: 2
Views: 4209
Reputation: 3495
TFilterType is a Java class. in this case you should use a faces converter for your type.
please try this sample
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>
converter:
@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 "";
}
}
}
Upvotes: 2