Recently, I encountered a problem in the project that I needed to compare the values of two string types on the JSP page. So I took it for granted to write the following code:
<c:if test="${‘p‘+longValue==‘p1‘}">some text</c:if>
Longvalue is a long value in requestscope. When you access the JSP page, an error is reported. Check the console and throw the numberformatexception.
java.lang.NumberFormatException: For input string: "p"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:410)
at java.lang.Long.valueOf(Long.java:525)
...
The error message shows that El tries to resolve the character "p" to a value of the long type and perform arithmetic operations to query the document. The following is a description:
All of the Binary Arithmetic Operations preferDoubleValues and all of them will resolve0If either of their operands isNull. The operators+-* %Will try to coerce their operandsLongValues if they cannot be coercedDouble.
From this we can know that the + operator is only an arithmetic operator in El and cannot be used for String concatenation. You can only use other methods.
Since the method of the object can be called directly in El, there isConcat(String Str) method. The return value is the result string after splicing Str.
In addition, the basic type can be automatically converted to the string type as the Concat input parameter, so the code is changed:
<c:if test="${‘p‘.concat(longValue)==‘p1‘}">some text</c:if>
The running result is normal.
Then, read the El 3.0 specification document. el3.0 provides the String concatenation operator "+ = ".
String concatenation operator
To evaluate
A += B
- Coerce a and B to string.
- Return the concatenated string of A and B.
Therefore, the following code can be used in el3.0:
<c:if test="${(‘p‘+=longValue)==‘p1‘}">some text</c:if>
String concatenation operator
To evaluate
A += B
- Coerce a and B to string.
- Return the concatenated string of A and B.
How to concatenate strings in El