參考我先前的問題(Programmatically create and add composite component in backing bean)我已成功地從backing bean添加複合組件。現在我遇到了一個新問題,因爲其中包含惰性數據的複合組件根本不會調用load()方法。有關於此的錯誤報告(https://code.google.com/p/primefaces/issues/detail?id=3258),但標記爲與PF3.0.RC1相關,並且如果它對於我正在使用的3.5版已修復,我不知道。以編程方式添加的複合組件內的數據表
我使用與可能性提到的完全相同的代碼BalusC到值表達式添加到複合材料部件:
public void includeCompositeComponent(UIComponent parent, String libraryName, String resourceName, String id, Map<String, String> valueExpressions) {
...
if (!valueExpressions.isEmpty()) {
ExpressionFactory factory = application.getExpressionFactory();
ELContext ctx = context.getELContext();
for (Map.Entry<String, String> entry : valueExpressions.entrySet()) {
ValueExpression expr = factory.createValueExpression(ctx, entry.getValue(), String.class);
composite.setValueExpression(entry.getKey(), expr);
}
}
...
}
這是我的複合材料構件testDatatable.xhtml:
<cc:interface>
<composite:attribute name="model" required="true" type="org.primefaces.model.LazyDataModel"/>
<composite:attribute name="id" default="dataTable"/>
</cc:interface>
<cc:implementation>
<h:form id="dataTableForm">
<p:dataTable id="#{cc.attrs.id}" value="#{cc.attrs.model}" var="test" paginator="true" rows="10"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15,20" lazy="true">
<p:column>
<h:outputText value="#{test.car.model}"/>
</p:column>
</p:dataTable>
</h:form>
</cc:implementation>
這裏是從支撐豆創建複合部件:
Map<String, String> v = new HashMap<String, String>();
v.put("model", "#{testBean.lazyModel}");
addCompositeComponent(rootPanel, "comp", "testDatatable.xhtml", "table", v);
這裏是懶惰的數據模型我加載在@PostConstruct
pulic class TestBean {
private TestLazyDataModel<TestClass> lazyModel;
@PostConstruct
public void init() {
lazyModel = new TestLazyDataModel();
}
public TestLazyDataModel<TestClass> getLazyModel() {
return lazyModel;
}
class TestLazyDataModel extends LazyDataModel<TestClass> {
@Override
public List<TestClass> load(int first, int pageSize, String sort, SortOrder sortOrder, Map<String, String> filters) {
List<TestClass> ret = new ArrayList<TestClass>();
TestClass t = new TestClass();
Car c = new Car("Volvo");
t.setCar(c);
ret.add(t);
return ret;
}
}
}
和一些輔助類:
public class TestClass {
private Car car;
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return car;
}
}
public class Car {
public String model;
public Car(String model) {
this.model = model;
}
public String getModel() {
return model;
}
}
總之作爲頁面現已開通:
- 這個bean創建延遲數據模型ok
- 從複合組件正確提取延遲數據模型
- 當數據表試圖通過在它的項目的錯誤循環:類「java.lang.String中」不具有財產「車」
改變<h:outputText value="#{test.car.model}"/>
到<h:outputText value="#{test.class.name}"/>
也java.lang.String中,但這次不會崩潰。調試時,TestLazyDataModel的load()方法永遠不會被調用。
還要使這甚至離奇的是,如果我使用相同的複合材料部件從任何其他XHTML頁面,如:
<comp:testDatatable model="#{anotherBean.model}">
它工作正常。我錯過了什麼?這是否與組件的渲染順序有關?任何幫助,高度讚賞!