在 JSP EL 中使用接口缺省方法时“在类型上找不到属性”
请考虑以下接口:
public interface I {
default String getProperty() {
return "...";
}
}
和实现类,它只是重用默认实现:
public final class C implements I {
// empty
}
每当在 JSP EL 脚本上下文中使用 的实例时:C
<jsp:useBean id = "c" class = "com.example.C" scope = "request"/>
${c.property}
-- 我收到一个 :PropertyNotFoundException
javax.el.PropertyNotFoundException: Property 'property' not found on type com.example.C
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:268)
javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:221)
javax.el.BeanELResolver.property(BeanELResolver.java:355)
javax.el.BeanELResolver.getValue(BeanELResolver.java:95)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
org.apache.jsp.index_jsp._jspService(index_jsp.java:225)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我最初的想法Tomcat 6.0对于Java 1.8功能来说太老了,但我惊讶地看到Tomcat 8.0也受到影响。当然,我可以通过显式调用默认实现来解决此问题:
@Override
public String getProperty() {
return I.super.getProperty();
}
- 但是为什么默认方法对Tomcat来说是一个问题?
更新:进一步的测试显示找不到默认属性,而默认方法可以找到,因此另一种解决方法(Tomcat 7 +)是:
<jsp:useBean id = "c" class = "com.example.C" scope = "request"/>
<%-- ${c.property} --%>
${c.getProperty()}