1

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}"
/>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49
  • 2
    How do you think this is a duplicate? The question you have linked uses specific frameworks like OmniFaces and PrimeFaces to solve the problem. The main thing here is how to construct a MethodExpression from code. I might be using it in a RichFaces widget, but that's just a usage example and can be replaced. The solution itself is pure JSF and works with all *Faces frameworks. – Benjamin Maurer Aug 01 '16 at 22:19

1 Answers1

3

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;
}
Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49