在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?
我正在将JSF 2.0与facelets一起使用.
我正在使用以下嵌入式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属性中是无效字符,因此您必须对它们进行转义(并指示<h:outputText>
不再通过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}" />
或者,您也可以使用<c:set>
为表达式加上别名:
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屋!