【发布时间】:2014-02-26 20:08:40
【问题描述】:
我正在使用 Mojarra JSF 编写我的自定义表格复合组件。我还试图将该复合材料绑定到支持组件。目的是能够在复合属性中指定表格的元素数量,稍后绑定的支持组件将在视图渲染之前自动生成元素本身。我有这个示例代码:
主页:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:comp="http://java.sun.com/jsf/composite/comp">
<h:head />
<body>
<h:form>
<comp:myTable itemNumber="2" />
</h:form>
</body>
</html>
myTable.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<composite:interface componentType="components.myTable">
<composite:attribute name="itemNumber"
type="java.lang.Integer" required="true" />
</composite:interface>
<composite:implementation>
<h:dataTable value="#{cc.values}" var="value">
<h:column headerText="column">
#{value}
<h:commandButton value="Action" action="#{cc.action}" />
</h:column>
</h:dataTable>
</composite:implementation>
</h:body>
</html>
MyTable.java:
@FacesComponent("components.myTable")
public class MyTable extends UINamingContainer {
private List<String> values = new ArrayList<String>();
public void action() {
System.out.println("Called");
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
// Initialize the list according to the element number
Integer num = (Integer) getAttributes().get("itemNumber");
for (int i = 0; i < num; i++) {
values.add("item" + i);
}
super.encodeBegin(context);
}
public List<String> getValues() {
return values;
}
}
问题是表格被正确渲染(在这种情况下有两个项目),但action 方法在按下行上的按钮时不会被调用。
如果我遵循wiki page 的复合组件,我可以让它以这种方式工作,但是每次调用getValues() 时都必须初始化List,从而在getter 方法中引入逻辑:-(。
对此有任何想法吗?这似乎是与覆盖 encodeBegin 方法有关的问题。我也尝试在markInitialState 上初始化它,但那里还没有属性...
使用 Mojarra 2.1.27 + Tomcat 6-7 和 Mojarra 2.2.5 + Tomcat 7 测试
【问题讨论】:
标签: jsf jsf-2 composite-component