Java EL 中的字符串连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3640254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Concatenation in EL
提问by Tom Tucker
I would like to concatenate a string within a ternary operator in EL(Expression Language).
我想在 EL(表达式语言)中的三元运算符中连接一个字符串。
Suppose there is a variable named value. If it's empty, I want to use some default text. Otherwise, I need to append it with some static text.
假设有一个名为 value 的变量。如果它是空的,我想使用一些默认文本。否则,我需要附加一些静态文本。
${(empty value)? "none" : value + " enabled"}
This will not compile however. What would be a correct way to write this? Or is this even possible?
但是,这不会编译。写这个的正确方法是什么?或者这甚至可能吗?
采纳答案by McDowell
This answer is obsolete. Technology has moved on. Unless you're working with legacy systems see Joel's answer.
这个答案已经过时了。技术已经向前发展。除非您使用的是遗留系统,否则请参阅Joel 的回答。
There is no string concatenation operator in EL. If you don't need the concatenated string to pass into some other operation, just put these expressions next to each other:
EL 中没有字符串连接运算符。如果不需要将连接的字符串传递给其他操作,只需将这些表达式并排放置:
${value}${(empty value)? 'none' : ' enabled'}
回答by BalusC
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:
如果您已经在使用 EL 3.0(Java EE 7;WildFly、Tomcat 8、GlassFish 4 等),那么您可以+=
为此使用 new运算符:
<c:out value="${empty value ? 'none' : value += ' enabled'}" />
If you're however not on EL 3.0 yet, and the value
is a genuine java.lang.String
instance (and thus not e.g. java.lang.Long
), then use EL 2.2 (Java EE 7; JBoss AS 6/7, Tomcat 7, GlassFish 3, etc) capability of invoking direct methods with arguments, which you then apply on String#concat()
:
但是,如果您还没有使用 EL 3.0,并且它value
是一个真正的java.lang.String
实例(因此不是 eg java.lang.Long
),则使用 EL 2.2(Java EE 7;JBoss AS 6/7、Tomcat 7、GlassFish 3 等)调用功能带参数的直接方法,然后您将其应用于String#concat()
:
<c:out value="${empty value ? 'none' : value.concat(' enabled')}" />
Or if you're even 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:
或者,如果您甚至还没有使用 EL 2.2,那么使用 JSTL<c:set>
创建一个新的 EL 变量,其中连接的值只是内联在 value 中:
<c:set var="enabled" value="${value} enabled" />
<c:out value="${empty value ? 'none' : enabled}" />
回答by Adalarasan Sachithanantham
1.The +(operator) has not effect to that in using EL. 2.so this is the way,to use that
1.+(operator)对使用EL没有影响。2.所以这是方法,使用那个
<c:set var="enabled" value="${value} enabled" />
<c:out value="${empty value ? 'none' : enabled}" />
is this helpful to You ?
这对你有帮助吗?
回答by Joel Richard
With EL 2 you can do the following:
使用 EL 2,您可以执行以下操作:
#{'this'.concat(' is').concat(' a').concat(' test!')}
回答by Aniket Kulkarni
Since Expression Language 3.0, it is valid to use += operator for string concatenation.
从 Expression Language 3.0 开始,使用 += 运算符进行字符串连接是有效的。
${(empty value)? "none" : value += " enabled"} // valid as of EL 3.0
Quoting EL 3.0 Specification.
引用EL 3.0 规范。
String Concatenation Operator
To evaluate
A += B
- Coerce A and B to String.
- Return the concatenated string of A and B.
字符串连接运算符
评估
A += B
- 强制 A 和 B 为字符串。
- 返回 A 和 B 的连接字符串。
回答by Mohammad Faisal
Mc Dowell's answeris right. I just want to add an improvement if in case you may need to return the variable's value as:
麦克道威尔的回答是正确的。如果您可能需要将变量的值返回为:
${ empty variable ? '<variable is empty>' : variable }
回答by Benjamin Fuentes
it also can be a great idea using concat for EL + MAP + JSON problem like in this example :
使用 concat 解决 EL + MAP + JSON 问题也是一个好主意,就像在这个例子中一样:
#{myMap[''.concat(myid)].content}
#{myMap[''.concat(myid)].content}