如何从 EL 调用对象的方法?
给对象:
public class TestObj {
public testObj() { };
public String test() { return "foo"; }
public String someOtherMethod(String param) { return param + "_bar"; }
}
然后obj被添加到pageContext
pageContext.setAttribute("t", new TestObj());
我将如何执行相当于:
<%= t.test() %>
<%= t.someOtherMethod("foo") %>
使用 EL?
最佳答案
自 2009 年 12 月 10 日发布的 EL 2.2 以来(已经超过 2.5 年了!),它就受到支持。 EL 2.2 与 Servlet 3.0 齐头并进,因此如果您将 Servlet 3.0 容器(Tomcat 7、Glassfish 3 等)与 Servlet 3.0 兼容的web.xml
作为目标,如下所示
<?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>
然后您将能够以下列形式在 EL 中调用带参数或不带参数的方法:
${t.test()}
${t.someOtherMethod('foo')}
关于java - 从 EL 执行和对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10678353/