【发布时间】:2013-01-11 01:36:03
【问题描述】:
您好,我想实现一个类似的 CC,它实现了一个 selection 属性。这类似于 Primefaces instant row selection <p:dataTable selection="#{bean.user}" .../>。
这是组合中的相关定义:
<composite:interface componentType="my.CcSelection">
<composite:attribute name="actionListener" required="true"
method-signature="void listener(javax.faces.event.AjaxBehaviorEvent)"/>
<composite:attribute name="selection"/>
</composite:interface>
<composite:implementation>
...
<ui:repeat var="item" value="#{cc.attrs.data}">
<p:commandLink actionListener="#{cc.actionListener(item)}"/>
</ui:repeat>
...
</composite:implementation>
backing bean 中的actionListener-方法如下:
public void actionListener(MyObject object)
{
logger.info("ActionListener "+object); //Always the correct object
FacesContext context = FacesContext.getCurrentInstance();
Iterator<String> it = this.getAttributes().keySet().iterator();
while(it.hasNext()) { //Only for debugging ...
logger.debug(it.next());
}
// I want the set the value here
ValueExpression veSelection = (ValueExpression)getAttributes().get("selection");
veSelection.setValue(context.getELContext(), object);
// And then call a method in a `@ManagedBean` (this works fine)
MethodExpression al = (MethodExpression) getAttributes().get("actionListener");
al.invoke(context.getELContext(), new Object[] {});
}
如果我将我的 CC 与 selection 的值表达式一起使用(例如,selection="#{testBean.user}"、veSelection 是 null(并且 selection 不会打印在调试迭代),我得到一个 NPE。如果我传递一个字符串(例如selection="test",属性 selection 在调试迭代中可用,但是(当然)我得到一个ClassCastException :
java.lang.String cannot be cast to javax.el.ValueExpression
如何为我的组件提供 actionListener 和 selection 属性,并在第一步中设置选定的值,然后调用 actionListener em>?
【问题讨论】:
标签: java jsf-2 composite-component