我正在尝试开发一个自定义组件,需要从backbean调用一个方法来从bb获取一些数据(这将在一个Ajax调用之后在解码阶段调用),并带有一个参数(它将进来ajax电话)。
我遇到的问题是我将属性定义为MethodExpression(在taglibrary和组件中),我得到Ajax帖子,解码参数,当我尝试从组件中获取Method绑定时以下错误:
javax.el.PropertyNotFoundException:/easyFaces.xhtml @ 19,151 dataSource =“#{theBean.loadDataFromSource}”:该类 'ar.com.easytech.faces.test.homeBean'没有该属性 'loadDataFromBean'。
以下是相关代码..(如果这不是正确的方法,请告诉我..)
标签库:
<attribute>
<display-name>Data Source</display-name>
<name>dataSource</name>
<required>true</required>
<type>javax.el.MethodExpression</type>
<method-signature>java.util.List theDataSource(java.lang.String)</method-signature>
</attribute>
组件定义:
public class Autocomplete extends HtmlInputText implements ClientBehaviorHolder
...
public MethodExpression getDataSource() {
return (MethodExpression) getStateHelper().eval(PropertyKeys.dataSource);
}
public void setDataSource(MethodExpression dataSource) {
getStateHelper().put(PropertyKeys.dataSource, dataSource);
}
最后生成错误的渲染方法:
private List<Object> getData(FacesContext context, Autocomplete autocomplete, String data) {
Object dataObject = null;
MethodExpression dataSource = autocomplete.getDataSource();
if (dataSource != null) {
try {
dataObject = dataSource.invoke(context.getELContext(), new Object[] {data});
return convertToList(dataObject);
} catch (MethodNotFoundException e) {
logger.log(Level.INFO,"Method not found: {0}", dataSource.getExpressionString() );
}
}
return null;
}
以下是BB的方法
public List<String> autcompleteFromSource(String param) {
List<String> tmpData = new ArrayList<String>();
tmpData.add("XXA_TABLE_A");
tmpData.add("XXA_TABLE_B");
tmpData.add("XXA_TABLE_C");
return tmpData;
}
带有组件的.xhtml
<et:autocomplete id="autoc" minLength="3" delay="500" value="#{easyfacesBean.selectedValue}" dataSource="#{easyfacesBean.autcompleteFromSource}" />
问题是如果我定义了一个方法getAutocompleteFromSource()它识别了方法并且错误更改为无法将列表转换为MethodExpression,所以显然它只是将autocompleteFromSource解释为一个简单属性而不是方法定义,这甚至是从BB调用方法的正确方法吗? (认为这不是实际行动或验证)
答案 0 :(得分:2)
我找到了解决方案,因为事实证明你还需要定义一个“处理程序”来定义方法签名,所以我创建了处理程序并添加到taglib,一切都开始工作正常。只是为了参考..这是处理程序..
此致
public class AutocompleteHandler extends ComponentHandler {
public AutocompleteHandler(ComponentConfig config) {
super(config);
}
protected MetaRuleset createMetaRuleset(Class type) {
MetaRuleset metaRuleset = super.createMetaRuleset(type);
metaRuleset.addRule(new MethodRule("dataSource", List.class, new Class[] { String.class }));
return metaRuleset;
}
}