Execution method and object from EL

How do I call an object method from EL?

Give the object:

public class TestObj {
   public testObj() { };

   public String test() { return "foo"; }
   public String someOtherMethod(String param) { return param + "_bar"; } 
}

and the obj object is added to pageContext

pageContext.setAttribute("t", new TestObj());

How to fulfill the equivalent:

<%= t.test() %>
<%= t.someOtherMethod("foo") %>

using EL?

+5
source share
2 answers

It was supported with EL 2.2, which was released on December 10, 2009 (more than 2.5 years ago!). EL 2.2 goes hand in hand with Servlet 3.0, so if you are targeting a Servlet 3.0 container ( Tomcat 7 , Glassfish 3 , etc.) with a compatible Servlet 3.0 web.xmlthat looks like this

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Config here. -->

</web-app>

then you can call methods with or without arguments in EL in the following forms:

${t.test()}
${t.someOtherMethod('foo')}
+8
source

EL Expression JSR . JST- JavaBean . , test :

public class TestObj {
    public TestObj() { };

    public String getTest() { return "foo"; }
}

getTest() :

${t.test}

, - , , EL. , , TLD. .

Update:

@BalusC, . , Java EE 6, Oracle , .

+3

All Articles