How to concatenate Strings in EL expression?

≯℡__Kan透↙ 提交于 2019-12-27 12:24:58

问题


I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:

I tried nesting an EL expression something like this:

<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />

However this failed with an EL exception. What is a right syntax/approach to do this?


回答1:


If you're already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this:

<h:commandButton ... action="#{someController.doSomething(id += 'SomeTableId')}" />

If you're however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():

<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />

Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:

<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />

See also:

  • String concatenation in EL for dynamic ResourceBundle key


来源:https://stackoverflow.com/questions/9680903/how-to-concatenate-strings-in-el-expression

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