如何在EL JSF中使用方括号 [英] How to use square bracket in EL JSF

查看:127
本文介绍了如何在EL JSF中使用方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have seen people using square bracket in JSF, and I am not sure if I understand its use correctly. So maybe an JSF guru can help me understand it

1.可以说我有这个

#{bean.x}

并且x是二维数组(x [] []),如何使用EL显示x[0]?我想在这种情况下我需要使用方括号.我想我使用#{bean.x[0]},但是出现异常.

and x is a two dimensional array (x[][]), how do I display x[0] using EL? I would imagine that I need to use square bracket in this case. I think I use #{bean.x[0]}, but I got exception.

2.第二种情况来自BalusC代码将参数传递给复合组件动作属性

2.The second scenario is from BalusC code Pass Argument to a composite-component action attribute

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

我了解代码在做什么,并且效果很好,但是如果有人可以解释这种情况下方括号的用途,我将不胜感激.非常感谢

I understand what the code is doing and it works beautifully, but I would appreciate if someone can explain what is the use of the square bracket in this case. Thank you very much

推荐答案

我想我使用#{bean.x[0]},但出现异常.

I think I use #{bean.x[0]}, but I got exception.

很遗憾,您没有共享例外详细信息.但是,只要有一个getX()方法返回给定索引确实存在的非null数组,这应该就可以了.

It's unfortunate that you didn't share the exception details. But this should just work, provided that there's a getX() method which returns a non-null array of which the given index really exists.

第二种情况是从BalusC代码传递参数到复合组件操作属性

在这种特殊情况下,大括号符号[]使您可以使用动态属性名称或操作方法名称.以下内容当然不起作用

In this particular case, the brace notation [] enables you to use a dynamic property name or action method name. The following of course don't work

#{cc.attrs.bean.cc.attrs.action}

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

Map<K, V>上也使用大括号符号.它允许您指定包含点的键(这些键又不应被EL评估为属性)

The brace notation is also used on Map<K, V>. It allows you to specify keys which contain dots (which in turn shouldn't be EL-evaluated as properties)

#{bean.map['key.with.dots']}

当然,它还允许您指定动态地图键:

It of course also allows you to specify a dynamic map key:

#{bean.map[otherBean.mapKey]}

另请参见:

  • 我们的EL Wiki页面
  • See also:

    • Our EL wiki page
    • 这篇关于如何在EL JSF中使用方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆