Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 875
Rajat Gupta Avatar asked Sep 12 '11 09:09

Rajat Gupta


People also ask

What is the correct way to concatenate the strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What is the symbol for concatenating strings?

The + (plus) operator is often overloaded to denote concatenation for string arguments: "Hello, " + "World" has the value "Hello, World" .

What is the best way to concatenate strings in JavaScript?

The + Operator The same + operator you use for adding two numbers can be used to concatenate two strings. You can also use += , where a += b is a shorthand for a = a + b . If the left hand side of the + operator is a string, JavaScript will coerce the right hand side to a string.


2 Answers

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.

like image 161
SlavaSt Avatar answered Sep 21 '22 04:09

SlavaSt


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" /> 
like image 36
BalusC Avatar answered Sep 23 '22 04:09

BalusC