Concatenating strings within EL expression defined in an attribute of a facelets tag

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?

I'm using JSF 2.0 with facelets.


EDIT :

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?

Answers


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}&lt;br/&gt;#{cc.attrs.count-1}" escape="false" rendered="#{cc.attrs.appreciatedByCurrentUser}" />
<h:outputText value="#{cc.attrs.count+1}&lt;br/&gt;#{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}&lt;br/&gt;#{cc.attrs.count-1}" />
<c:set var="notAppreciated" value="#{cc.attrs.count+1}&lt;br/&gt;#{cc.attrs.count}" />
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? appreciated : notAppreciated}" escape="false" />

It is possible to concatenate Strings in EL using the java.lang.String.concat(String) method. Thus your code could look like:

<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ?  (''.concat(cc.attrs.count).concat('&lt;br/&gt;').concat(cc.attrs.count-1)) :  (''.concat((cc.attrs.count+1)).concat('&lt;br/&gt;').concat(cc.attrs.count))}" escape="false" />

In this particular case however I would go with one of the options that Mr BalusC had suggested because the code above doesn't look quite elegant. In some cases knowing this technique could be useful, though.

I would hardly recommend using javascript as a workaround here.


This is the only thing i can come up with.

<h:panelGroup rendered="#{cc.attrs.appreciatedByCurrentUser}">
   <h:outputText value="#{(cc.attrs.count)}" style="display:block;" />
   <h:outputText value="#{(cc.attrs.count-1)}" />
</h:panelGroup>
<h:panelGroup rendered="#{not cc.attrs.appreciatedByCurrentUser}">
   <h:outputText value="#{(cc.attrs.count+1)}" style="display:block;" />
   <h:outputText value="#{(cc.attrs.count)}" />
</h:panelGroup>

Putting <br> in a value attribute will always throw errors in JSF, so you'll have to use display:block.


Need Your Help

ng-repeat specify a starting index

angularjs angularjs-ng-repeat

How can I specify an index where an ng-repeat directive is going to start instead of zero?

Segue from one storyboard to a different storyboard?

ios xcode storyboard segue

I have too many views on one storyboard which is causing it to run really slow. I have been told that a solution to this issue would be to split the one storyboard into multiple storyboards. Could ...