Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate Strings in EL expression?

Tags:

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:

I tried nesting an EL expression something like this:

<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" /> 

However this failed with an EL exception. What is a right syntax/approach to do this?

like image 213
Benjamin Harel Avatar asked Mar 13 '12 08:03

Benjamin Harel


People also ask

What is the correct way to concatenate the strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.

What is the string concatenation operator in awk?

6.2. 2 String Concatenation It does not have a specific operator to represent it. Instead, concatenation is performed by writing expressions next to one another, with no operator. For example: $ awk '{ print "Field number one: " $1 }' mail-list -| Field number one: Amelia -| Field number one: Anthony …

Can you use += for string concatenation?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

How do I concatenate two characters to a string?

2) String Concatenation by concat() method The String concat() method concatenates the specified string to the end of current string. Syntax: public String concat(String another)


1 Answers

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:

<h:commandButton ... action="#{someController.doSomething(id += 'SomeTableId')}" /> 

If you're however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():

<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" /> 

Or if you're 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:

<c:set var="tableId" value="#{id}SomeTableId" /> <h:commandButton ... action="#{someController.doSomething(tableId)}" /> 

See also:

  • String concatenation in EL for dynamic ResourceBundle key
like image 148
BalusC Avatar answered Sep 29 '22 23:09

BalusC