I've been trying to get a "generic" dialog, with autocomplete functionality, to work for a few days now. Turns out, I was just creating the MethodExpression the "wrong way". So I thought I'd document this here.
To reiterate: You want to dynamically create a MethodExpression, store it in a Property and use it in a JSTL Template or JSF Page.
For example:
// Template
<c:forEach items="#{property.subItems}" var="subitem">
<ui:include src="editor.xhtml">
<ui:param name="autocompleteMethod" value="#{subitem.autocompMethod}" />
</ui:include>
</c:forEach>
// editor.xhtml
// We're using RichFaces (unfortunately), but this is just an example
<rich:autocomplete mode="cachedAjax" minChars="2"
autocompleteMethod="#{autocompleteMethod}"
/>
I've found the solution at http://javaevangelist.blogspot.co.at/2012/10/jsf-2x-tip-of-day-programmatically_20.html
public static MethodExpression createMethodExpression(String methodExpression, Class<?> expectedReturnType, Class<?>[] expectedParamTypes) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), methodExpression, expectedReturnType, expectedParamTypes);
}
You can then create a MethodExpression and store it in a property. For RichFaces autocomplete, the Signature is: List<String> autocomplete(String prefix)
@SuppressWarnings("rawtypes") // Generics use type erasure
Class<List> retType = List.class;
Class<?>[] paramTypes = {String.class};
MethodExpression autocompleteMethod = createMethodExpression("#{myBean.myAutocomplete}", retType, paramTypes);
// In the questions example, we'd need to set a property here:
this.autocompMethod = autocompleteMethod;
Then have the apropriate getter:
MethodExpression getAutocompMethod() {
return this.autocompMethod;
}