How to concatenate strings in El

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.