如何修复此异常java.lang.String不能强制转换为用户定义的类型



伙计们,我正在尝试遍历用户定义的对象列表,但我遇到了这个错误(java.lang.String不能转换为bg.fmi.master.thestic.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,tRquestFilters=[]],TFilterType[filter TypeName=Украса,filterDescriptc=装饰,isBoolean Type=B,tRequestFilters=[]]]

提前感谢!

TFilterType是一个Java类。在这种情况下,您应该为您的类型使用面转换器
请试用这个样品

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 "";
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新