【问题标题】:JSF2 Converting List of String to String[]JSF2 将字符串列表转换为字符串 []
【发布时间】:2011-08-04 17:25:44
【问题描述】:

有点紧张。我需要将列表转换为字符串数组:

List<String> selectedIndicieString[] targetIndices;

selectedIndicie 列表是从支持 bean 中提取的,我尝试将其转换为 String 数组,如下所示:

setTargetIndices(initialiseBean.getSelectedIndicie().toArray(getTargetIndices())); 

但是 Java 有句老话:

在托管 bean searchBean 上执行资源注入时发生错误

正如我所说,我正在讨论如何将字符串列表转换为字符串数组,因此任何建议都会很可爱。

干杯

【问题讨论】:

  • 你能把线路拆分一下,看看哪个呼叫失败了吗?
  • “在托管 bean searchBean 上执行资源注入时出错”。请阅读服务器日志以了解整个堆栈跟踪的根本原因。它包含你的答案。
  • 你是在构造函数中这样做的吗?如果是这样,请尝试将其移至带有 @PostConstruct 的方法。
  • @BalusC:曾经我收到过类似的信息。现在,我记得实际上我试图在 ManagedBean 构造函数中使用 EJB。那是另一种情况。

标签: java jsf-2


【解决方案1】:

List.toArray 返回一个Objects 的数组,当您执行setTargetIndices 时可能会导致异常,因为此方法将接受String 对象的数组。答案是不要将 setter 转换为接受 Object 的数组(因为 JSF 运行时可能根本无法将 setter 识别为属于 targetIndices 属性),而是使用字符串数组调用 setter .

【讨论】:

  • 扳手时刻结束:String[] targetIndices = initialiseBean.getSelectedIndicie().toArray(new String[0]);
  • 我想知道你是如何设法克服编译器错误的,并且正要删除这个答案。
【解决方案2】:

正如我所说,我正在讨论如何转换列表 将字符串转换为字符串数组,因此任何建议都会很可爱。

使用toArray 方法。

【讨论】:

    【解决方案3】:

    试试这个:

    String[] targetIndices = selectedIndicie.toArray(new String[selectedIndicie.size()]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2021-11-25
      • 2013-07-04
      • 1970-01-01
      相关资源
      最近更新 更多