如何在EL表达式中串联字符串? [英] How to concatenate Strings in EL expression?
问题描述
我需要为<h:commandButton>
创建一个回调,而作为参数,我需要传递一个由字符串连接且带有外部参数id的参数:
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:
我尝试嵌套类似这样的EL表达式:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
但是,此操作因EL异常而失败.什么是正确的语法/方法来做到这一点?
However this failed with an EL exception. What is a right syntax/approach to do this?
推荐答案
如果您已经在使用EL 3.0(Java EE 7; WildFly,Tomcat 8,GlassFish 4等),则可以使用新的+=
运算符:
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')}" />
但是,如果您尚未使用EL 3.0,并且左手是真正的java.lang.String
实例(因此不是例如java.lang.Long
),请使用EL 2.2功能调用带参数的直接方法,然后应用于String#concat()
:
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'))}" />
或者,如果您尚未使用EL 2.2,请使用JSTL <c:set>
创建一个新的EL变量,其串联值仅内嵌在value中:
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)}" />
另请参见:
- EL中用于动态ResourceBundle键的字符串连接
- String concatenation in EL for dynamic ResourceBundle key
See also:
这篇关于如何在EL表达式中串联字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!