【问题标题】:Create MethodExpression in Java (and use in JSF) [duplicate]在 Java 中创建 MethodExpression(并在 JSF 中使用)[重复]
【发布时间】:2016-12-06 19:22:21
【问题描述】:

几天来,我一直在尝试获得一个具有自动完成功能的“通用”对话框。事实证明,我只是以“错误的方式”创建 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}"
/>

【问题讨论】:

  • 你觉得这是复制品吗?您链接的问题使用 OmniFaces 和 PrimeFaces 等特定框架来解决问题。这里主要是如何从代码构造 MethodExpression。我可能在 RichFaces 小部件中使用它,但这只是一个使用示例,可​​以替换。该解决方案本身是纯 JSF 并适用于所有 *Faces 框架。

标签: jsf richfaces el uiinclude methodexpression


【解决方案1】:

我在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&lt;String&gt; 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;
}

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 2012-12-19
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2017-04-19
    • 2018-06-11
    相关资源
    最近更新 更多