How to use square bracket in EL JSF

浪尽此生 提交于 2019-12-06 06:26:26

问题


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.So let say I have this

#{bean.x}

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


回答1:


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

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.


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

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}

It would only try to invoke bean.getCc().getAttrs().action().

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

See also:

  • Our EL wiki page


来源:https://stackoverflow.com/questions/12343410/how-to-use-square-bracket-in-el-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!