Call methods in Expression Language
Asked Answered
H

2

10

How can I invoke a method in JSP using EL (Expression Language)?

For example:

<c:forEach var="item" items="${listStr}" begin="0" end="2" step="2">
    <p>${item.indexOf("h")}</p>
</c:forEach>

Here, the item is an object of type java.lang.String. This causes errors when run on Tomcat.

Hussar answered 30/6, 2012 at 11:14 Comment(1)
what is the error you are gettingDreda
S
17

This is only supported since EL 2.2. This goes in turn hand in hand with Servlet 3.0. So if you make sure that you target a Servlet 3.0 compatible container (Tomcat 7, Glassfish 3, etc) with a Servlet 3.0 compatible web.xml declaration, then it'll just work the way you intend. If you're however targeting an older versioned servlet container, or have an older versioned or even the wrong web.xml root declaration, then it won't work.

See also:


Update: Your particular problem is likely caused by a bug in Tomcat 7. It doesn't find the right overloaded method based on the given argument. Your expression works fine in Glassfish 3. All non-overloaded methods should work just fine in Tomcat.

Scilla answered 30/6, 2012 at 11:43 Comment(4)
I use Tomcat 7 , servlet 3.0 , but some method work , the other no . ex : indexOf(104) is ok , but indexOf("h") isn't . ("h" ascii is 104) .Hussar
This is a bug in Tomcat 7 issues.apache.org/bugzilla/show_bug.cgi?id=56147Savaii
From which version can I invoke not get methods without arguments?Callimachus
@PhilipRego: Ctrl+F on "findAttribute" in your link. You'll see it's just in superclass.Scilla
E
0

I can't get it to work either(Apache Tomcat/7.0.27).

javax.el.ELException: Cannot convert h of type class java.lang.String to int

El refuses to see the method with the correct signature. Same thing happens with indexOf("h",0). I did have success with

${x.contains("h")}
${x.lastIndexOf("h")}
${x.indexOf(104)}  Ascii value of 'h' is 104
Educator answered 30/6, 2012 at 15:21 Comment(2)
Yes , some method is ok . the other not . :| .Hussar
Have you tried sending 'h' (a char)? char can be translated to int.Dispenser

© 2022 - 2024 — McMap. All rights reserved.