参考我之前的问题(Programmatically create and add composite component in backing bean),我已成功地从支持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>
这是从backing bean创建复合组件:
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;
}
}
总之,现在页面已打开:
将<h:outputText value="#{test.car.model}"/>
更改为<h:outputText value="#{test.class.name}"/>
也是java.lang.String,但这次不会崩溃。调试时,永远不会调用TestLazyDataModel的load()方法。
另外,如果我使用与任何其他xhtml页面相同的复合组件,那么更奇怪的是:
<comp:testDatatable model="#{anotherBean.model}">
它工作正常。我错过了什么?这是否与组件的渲染顺序有关?任何帮助都非常感谢!
答案 0 :(得分:3)
刚刚开始工作..更改复合组件值表达式包括使用Object.class而不是String.class的代码:
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(), Object.class);
composite.setValueExpression(entry.getKey(), expr);
}
}
...
}
做了这个伎俩。希望这也有助于其他人!