如何从EL调用对象的方法?
给对象:
public class TestObj {
public testObj() { };
public String test() { return "foo"; }
public String someOtherMethod(String param) { return param + "_bar"; }
}
Run Code Online (Sandbox Code Playgroud)
并将obj添加到pageContext中
pageContext.setAttribute("t", new TestObj());
Run Code Online (Sandbox Code Playgroud)
我将如何执行相当于:
<%= t.test() %>
<%= t.someOtherMethod("foo") %>
Run Code Online (Sandbox Code Playgroud)
使用EL?
自从2009年12月10日(已超过2.5年前)已经推出的EL 2.2以来,它得到了支持.EL 2.2与Servlet 3.0密切相关,因此如果您使用兼容Servlet 3.0的Servlet 3.0容器(Tomcat 7,Glassfish 3等),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>
Run Code Online (Sandbox Code Playgroud)
那么你将能够以下列形式在EL中调用带或不带参数的方法:
${t.test()}
${t.someOtherMethod('foo')}
Run Code Online (Sandbox Code Playgroud)