在 facelets 标记的属性中定义的 EL 表达式中连接字符串 [英] Concatenating strings within EL expression defined in an attribute of a facelets tag
问题描述
我需要为这样的属性编写一个 EL 表达式:
I need to write an EL expression for an attribute which goes something like this:
#{cc.attrs.appreciatedByCurrentUser ? (cc.attrs.count +'<br/>'+ (cc.attrs.count-1)) : ((cc.attrs.count+1) +'<br/>'+ cc.attrs.count)}
现在的问题是,这会产生错误,因为字符串无法连接,就像我这样做的方式.那么我该如何解决这个问题?
Now the problem is that this gives an error as strings cannot be concatenated, the way I am doing it. So how can I rectify this?
我使用的是带有 facelets 的 JSF 2.0.
我正在使用以下内联 javascript 解决问题
I'm resolving the issue using the following inline javascript
<script type="text/javascript">
var count=#{cc.attrs.count};
document.write(#{cc.attrs.appreciatedByCurrentUser} ? (count-1) +'<br/>'+count : count+'<br/>'+ (count+1));
</script>
你能想到这方面的任何问题吗?
Can you think of any issue with this?
推荐答案
EL 中的字符串连接只能通过在表达式中内联来实现.+
运算符在 EL 中只是一个求和运算符.此外,<
和 >
是 XML 属性中的无效字符,因此您必须对它们进行转义(并指示 escape="false"
):
String concatenation in EL is only possible by just inlining in the expression. The +
operator is in EL exclusively a sum operator. Further, <
and >
are invalid characters in XML attributes, so you have to escape them (and instruct <h:outputText>
to not escape them once again by escape="false"
):
<h:outputText value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" escape="false" rendered="#{cc.attrs.appreciatedByCurrentUser}" />
<h:outputText value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" escape="false" rendered="#{!cc.attrs.appreciatedByCurrentUser}" />
或者,您也可以使用
为表达式添加别名:
Alternatively, you can also use <c:set>
to alias the expression:
<c:set var="appreciated" value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" />
<c:set var="notAppreciated" value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" />
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? appreciated : notAppreciated}" escape="false" />
这篇关于在 facelets 标记的属性中定义的 EL 表达式中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!