如何解决此异常,无法将java.lang.String强制转换为用户定义的类型 [英] How to fix this exception java.lang.String cannot be cast to user defined type
问题描述
伙计们,我正在尝试遍历用户定义对象的列表,但是却收到此错误(java.lang.String无法转换为bg.fmi.master.thesis.model.TFilterType),我无法弄清楚为什么.
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.
我的.xhtml我有:
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>
来自bean类的部分:
Part from the bean class:
private List<TFilterType> selectedBooleanFilterTypes;
public List<TFilterType> getSelectedBooleanFilterTypes() {
return selectedBooleanFilterTypes;
}
public void setSelectedBooleanFilterTypes(
List<TFilterType> selectedBooleanFilterTypes) {
this.selectedBooleanFilterTypes = selectedBooleanFilterTypes;
}
这是另一种方法的一部分,但在bean类中也是如此:
This is part from another method, but also in the bean class:
for (TFilterType type : selectedBooleanFilterTypes) {
System.out.println("SelectedFilterTypes: "
+ type.getFilterTypeName());
}
在调试模式下,我可以看到selectedBooleanFilterTypes具有以下值:
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 = []]
[TFilterType [filterTypeName=DJ, filterTypeDesc=DJ, isBooleanType=B, tRequestFilters=[]], TFilterType [filterTypeName=Украса, filterTypeDesc=Decoration, isBooleanType=B, tRequestFilters=[]]]
提前谢谢!
推荐答案
TFilterType是Java类.在这种情况下,您应该使用Faces Converter作为您的类型.
请尝试以下示例
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>
转换器:
@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 "";
}
}
}
这篇关于如何解决此异常,无法将java.lang.String强制转换为用户定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!