Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call methods in Expression Language

Tags:

jsp

tomcat

el

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.

like image 887
Trung Huynh Avatar asked Jun 30 '12 11:06

Trung Huynh


1 Answers

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:

  • Our EL wiki page

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.

like image 95
BalusC Avatar answered Oct 31 '22 05:10

BalusC