首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java创建MethodExpression (并在JSF中使用)

用Java创建MethodExpression (并在JSF中使用)
EN

Stack Overflow用户
提问于 2016-08-01 16:11:50
回答 1查看 1.8K关注 0票数 1

几天来,我一直试图获得一个具有自动完成功能的“通用”对话框。结果,我只是以“错误的方式”创建了MethodExpression。所以我想我应该把这个记录在这里。

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

例如:

代码语言:javascript
复制
// 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}"
/>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-01 16:11:50

我在20.html找到了解决方案

代码语言:javascript
复制
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)

代码语言:javascript
复制
@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;

那就有一个合适的吸气剂:

代码语言:javascript
复制
MethodExpression getAutocompMethod() {
    return this.autocompMethod;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38703477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档