• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about EL in HFSJ

 
dahooo chang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in HFSJ page 377:

why would compiler translate ${musicList[ nums[0] + 1] } into
${musicList["2"]} instead of
${musicList["11"]}?
I thought String concatenation would be of higher priority than primitive addition.
[ December 20, 2005: Message edited by: dahooo chang ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have a look at the spec : JSP.2.3.5.1 Binary operators - A {+,-,*} B
....
If A or B is BigInteger, coerce both to BigInteger and then:
If operator is +, return A.add( B )
....
Otherwise coerce both A and B to Long and apply operator

The String will be converted first, so the concatenation will not occur.

Btw, you meant ${musicList["2"]}, right ?
[ December 20, 2005: Message edited by: Satou kurinosuke ]
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dahooo chang:


I thought String concatenation would be of higher priority than primitive addition.



Can I just point out that EL does not overload the + operator for string concatenation; EL interprets + only as the numeric addition operator. So, operator precedence with + really shouldn't be an issue. I've never read HF, but I can't believe they would have missed the fact that + cannot be used for string concatenation in EL... It's one of those intuitive Java things which backfires on you!

It's interesting that the Apache JEXL project (http://jakarta.apache.org/commons/jexl/) incorporates the overloading of +, but it isn't a compatible implementation of EL as defined in JSP 2.0!

To do concatenation in compliant JSP 2.0 containers (or in JSTL 1.0 tag attributes), you should use two EL adjacent expressions. For example, to concatenate 'myvar' to 'myvar2' you would do:



and not:



which is illegal unless we want to perform numerical addition on the numbers 'myvar' and 'myvar2'.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic