1

我一直试图让一个具有自动完成功能的“通用”对话框工作几天。事实证明,我只是以“错误的方式”创建 MethodExpression。所以我想我会在这里记录下来。

重申一下:您希望动态创建 MethodExpression,将其存储在 Property 中并在 JSTL 模板或 JSF 页面中使用它。

例如:

// 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}"
/>
4

1 回答 1

3

我在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);
}

然后,您可以创建 MethodExpression 并将其存储在属性中。对于 RichFaces 自动完成,签名是: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;

然后有适当的吸气剂:

MethodExpression getAutocompMethod() {
    return this.autocompMethod;
}
于 2016-08-01T16:11:50.993 回答