java 强制转换 string,如何解决此异常java.lang.String无法强制转换为用户定义的类型...

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:

value="#{requestBean.selectedBooleanFilterTypes}"

layout="pageDirection">

value="#{filterTypeBean.listBooleanFilterTypes()}"

itemLabel="#{checkbox.filterTypeName}" itemValue="#{checkbox}" />

Part from the bean class:

private List selectedBooleanFilterTypes;

public List getSelectedBooleanFilterTypes() {

return selectedBooleanFilterTypes;

}

public void setSelectedBooleanFilterTypes(

List 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!

解决方案

TFilterType is a Java class. in this case you should use a faces converter for your type.

please try this sample

xhtml:

layout="pageDirection" converter="filterTypeConverter">

itemLabel="#{checkbox.filterTypeName}" itemValue="#{checkbox}"/>

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值