有一个问题让我发疯。我正在尝试显示来自复合组件的值。但它永远不会被打印出来。
<cc:interface>
<cc:attribute name="parts" type="java.util.List" required="true""/>
<cc:attribute name="context" type="String" required="true""/>
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
...
<c:forEach items="#{cc.attrs.parts}" var="part">
...
<td>#{part.roles(cc.attrs.context)}</td>
...
</c:forEach>
...
</cc:implementation>
后面的方法#{part.roles(cc.attrs.context)}
永远不会被调用(没有错误,没有打印)。
如果我使用#{part.roles("Example")}
我获得:
javax.el.ELException: /resources/utils/parts.xhtml: Method not found: class com.ex.Part.roles(java.lang.String)
两者parts
都有context
值(我可以打印它们)并调用#{part.roles}
确实打印了 HashMap。我错过了什么吗?
public class Part implements Serializable {
private HashMap<String, String> roles;
....
// This can be called OK
public HashMap<String, String> getRoles() {
return roles;
}
public String getRoles(String id) {
//Never called
if ((roles != null) && (id != null)) {
return roles.get(id);
}
return null;
}
}
编辑
第一个错误是调用部分应该是:
#{part.getRoles(cc.attrs.context)}
所以使用#{part.getRoles("Hello")}
至少正确调用方法。但是仍然cc.attrs.context
不能正确地作为参数渲染(该方法没有被调用),即使它渲染得很好#{cc.attrs.context}
。
编辑 2
更有趣。这有效
<c:set var="ci" value="#{cc.attrs.context}" scope="request"/>
<td>#{part.getRoles(ci)}</td>
我想知道这是否只是一个错误
编辑 3
我正在使用 TomEE 1.7.4。
在我的pom.xml
我有:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version> <!-- TomEE only supports 6.0 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.13</version>
<scope>runtime</scope>
</dependency>
我不确定为什么需要第二个依赖项(TomEE 应该就足够了),但是没有它,程序会给我这个错误:
Could not get component metadata for xxx.xhtml. Did you forget to specify <composite:interface>?