您好,我想实现一个 CC 类似的实现选择属性。这类似于 Primefaces 的即时行选择 <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>
actionListener
支持 bean 中的 - 方法如下:
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="#{testBean.user}"
,veSelection
是null
(并且在调试迭代中未打印选择)并且我得到 NPE。如果我传递一个字符串(例如selection="test"
,属性选择在调试中可用-迭代,但(当然)我得到一个ClassCastException
:
java.lang.String cannot be cast to javax.el.ValueExpression
如何为我的组件提供actionListener和selection属性,并在第一步中设置选定的值,然后调用actionListener?