如何在EL JSF中使用方括号

Tha*_*ham 3 jsf el jsf-2

我见过人们在JSF中使用方括号,我不确定我是否理解它的正确用法.所以也许JSF大师可以帮助我理解它

我可以说我有这个

#{bean.x}
Run Code Online (Sandbox Code Playgroud)

和x是二维数组(x [] []),如何x[0]使用EL 显示?我想在这种情况下我需要使用方括号.我想我用#{bean.x[0]},但我得到了例外.

2.第二种情况是从BalusC代码Pass Argument到复合组件action属性

<composite:interface>
   <composite:attribute name="bean" type="java.lang.Object" />
   <composite:attribute name="action" type="java.lang.String" />
   <composite:attribute name="property" type="java.lang.String" />
</composite:interface>
<composite:implementation>
   <h:commandButton value="Remove" action="#{cc.attrs.bean[cc.attrs.action]}">
      <f:setPropertyActionListener target="#{cc.attrs.bean[cc.attrs.property]}" value="Somestring" />
   </h:commandButton>
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

我理解代码正在做什么并且它工作得很漂亮,但如果有人能解释在这种情况下方括号的用途我会很感激.非常感谢你

Bal*_*usC 8

我想我用#{bean.x[0]},但我得到了例外.

不幸的是你没有分享异常细节.但这应该可行,只要有一个getX()方法可以返回null给定索引确实存在的非数组.


第二种情况是从BalusC代码Pass Argument到复合组件action属性

在此特定情况下,括号表示法[]允许您使用动态属性名称或操作方法名称.以下当然不起作用

#{cc.attrs.bean.cc.attrs.action}
Run Code Online (Sandbox Code Playgroud)

它只会尝试调用bean.getCc().getAttrs().action().

括号符号也用于Map<K, V>.它允许您指定包含点的键(反过来不应将EL评估为属性)

#{bean.map['key.with.dots']}
Run Code Online (Sandbox Code Playgroud)

它当然还允许您指定动态映射键:

#{bean.map[otherBean.mapKey]}
Run Code Online (Sandbox Code Playgroud)

也可以看看: