【问题标题】:java.lang.String cannot be cast to org.riediger.teams.logic.to.TeamTO [duplicate]java.lang.String 无法转换为 org.riediger.teams.logic.to.TeamTO [重复]
【发布时间】:2015-12-21 10:53:56
【问题描述】:

我现在有一个奇怪的问题(见问题标题)。它说即使不需要强制转换,它也无法转换到所述对象。

JSF 页面:http://pastebin.com/0aFNWc4h 豆:http://pastebin.com/vtA552fA

SelectManyCheckbox 的每个选定项都应作为 TeamTO 对象存储在此列表中。

Bean中的字段声明:

private List<TeamTO> preSelectionRecipientsTeams;

来自 jsf 页面的片段:

<h:selectManyCheckbox id="preSelectionRecipientsTeams" value="#{sendMailBean.preSelectionRecipientsTeams}">

在尝试迭代此列表时

for (TeamTO t : getPreSelectionRecipientsTeams()) {...}

我得到所说的错误。 有什么想法吗?

【问题讨论】:

  • 您是否尝试调试以查看列表中实际上是哪个对象?有一种情况是它在代码的其他地方填充了字符串。
  • 邮政编码这里...而不是其他地方...并将其设为minimal reproducible example
  • "System.out.println(getPreSelectionRecipientsTeams());"打印“信息:[TeamTO#2, TeamTO#10]”(通过 SelectManyCheckbox 选择某些对象后的正确行为)
  • @Kukeltje 为什么要删除 jsf 标签?它似乎适用于我。
  • 对不起,我没有检查代码(我从不查看外部文件)并且有一个 jsf 页面......所以它确实可能与 jsf 相关。抱歉,我会重新添加它

标签: java jsf classcastexception


【解决方案1】:
getPreSelectionRecipientsTeams()

包含字符串,尽管从它的类型来看它不应该。

找到它从中获取内容的地方并修复它。

根据您更新的问题,我推断如下:

您的 bean 包含所需类型的集合,但您尚未注册转换器以使 JSF 实际转换字符串,它通过 http 接收到这些对象。

所以:编写一个转换器并将其注册到您的 JSF 组件中。

开始阅读这一切的一个好点是:http://download.oracle.com/otn_hosted_doc/jdeveloper/j2ee101302/jsf_apps/eventvalidate/sf_avc_process.html

【讨论】:

  • preSelectionRecipientsTeams 是一个字符串列表。 private List&lt;TeamTO&gt; preSelectionRecipientsTeams; SelectManyCheckbox 的选定项应作为 TeamTO 存储在所述列表中。 &lt;h:selectManyCheckbox id="preSelectionRecipientsTeams" value="#{sendMailBean.preSelectionRecipientsTeams}"&gt; 我在监督一些特定于 jsf 的类型转换吗?
  • 抱歉,我什至没有尝试破译 cmets 中的代码。如果您认为它与问题相关,请将其放在问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多