【问题标题】:How to construct dynamically attribute name in xhtml - JSF如何在xhtml中动态构造属性名称 - JSF
【发布时间】:2015-10-27 17:08:33
【问题描述】:

我想为 xhtml 页面中的输入分配一个动态值。 ManagedBean 包含 3 个字符串属性:customField1、customField2 和 customField3

在 xhtml 页面中,我将值列表循环到 custruct 组件:

<ui:repeat  value="#{listBean.customFields}" var="item" varStatus="status">
<div >
    <p:outputLabel value="#{item.label}" />
    <br />
    <c:set var="test" value="#{'myBean.customField'.concat(status.index)}"/>

    <p:inputText value="#{test}"   />
</div>
</ui:repeat>

我为连接所做的操作不起作用,因为它认为整个表达式是一个字符串,因此它无法将“#{test}”与 bean 属性绑定。

您认为在 jsf 中可以做到这一点?

提前致谢!

【问题讨论】:

  • 你不能只做value="#{myBean.customField}#{status.index}"
  • 我试过了..它不起作用 =>javax.el.PropertyNotFoundException: page.xhtml value="#{myBean.customField}#{status.index}": 类 'com. xxx.yyy.zzz.MyBean' 没有属性 'customField'
  • customField 在您的支持 bean 中是否有公共 getter/setter?
  • 支持 bean 仅包含 customField1、customField2 和 customField3 以及公共 getter 和 setter
  • 请忽略 j.con 的 cmets。他显然是带着 [java] 标签(Kukeltje 理所当然地删除了它)来到这里的,只是在做盲目的猜测。在未来的 JSF 问题中,请不要使用 [java] 标签。

标签: jsf jstl managed-bean


【解决方案1】:

请尝试

<p:inputText value="#{myBean['customField'.concat(status.index)]}"/>

【讨论】:

  • 感谢您的回答..这不起作用:javax.el.PropertyNotFoundException: page.xhtml value="#{myBean.customField}#{status.index}": 类 'com. xxx.yyy.zzz.MyBean' 没有属性 'customField'。顺便说一句,在 xhtml 上有一条关于 EL 语法的警告,它从 concat 开始
  • 您的错误信息与答案中的代码不匹配。
  • 对不起!你说的对 !我认为它可以正确绑定。但是,由于我只有 customField1、customField2 和 customField3,所以我输入了 #{myBean['customField'.concat(status.index+1)]} 并收到此错误消息: The class 'com.xxx.yyy.zzz.MyBean ' 没有属性 'customField4'。请注意,customFields 仅包含 3 个元素...尽管列表的长度 = 3,但我不明白为什么直到 customField4 才会出现。显示 EL 表达式时,我得到了 1 、 2 和 3 !我很困惑..为什么它在绑定中进行额外的迭代?
猜你喜欢
  • 1970-01-01
  • 2018-05-22
  • 2012-08-21
  • 1970-01-01
  • 2014-03-10
  • 2013-01-29
  • 1970-01-01
  • 2011-07-12
相关资源
最近更新 更多