在Java中创建MethodExpression(并在JSF中使用)
几天来,我一直在尝试创建一个带有自动完成功能的“通用”对话框。事实证明,我只是用“错误的方式”创建了MethodExpression。所以我想我应该把这个记录在这里 重申:您希望动态创建MethodExpression,将其存储在属性中,并在JSTL模板或JSF页面中使用它 例如:在Java中创建MethodExpression(并在JSF中使用),jsf,richfaces,el,uiinclude,methodexpression,Jsf,Richfaces,El,Uiinclude,Methodexpression,几天来,我一直在尝试创建一个带有自动完成功能的“通用”对话框。事实证明,我只是用“错误的方式”创建了MethodExpression。所以我想我应该把这个记录在这里 重申:您希望动态创建MethodExpression,将其存储在属性中,并在JSTL模板或JSF页面中使用它 例如: // Template <c:forEach items="#{property.subItems}" var="subitem"> <ui:include src="editor.xhtml"
// 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}"
/>
//模板
//editor.xhtml
//我们正在使用RichFaces(不幸的是),但这只是一个例子
我在
你怎么认为这是一个复制品?您链接的问题使用OmniFaces和PrimeFaces等特定框架来解决问题。这里的主要内容是如何从代码构造MethodExpression。我可能在RichFaces小部件中使用它,但这只是一个使用示例,可以替换。该解决方案本身是纯JSF,可与所有*Faces框架一起使用。
public static MethodExpression createMethodExpression(String methodExpression, Class<?> expectedReturnType, Class<?>[] expectedParamTypes) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), methodExpression, expectedReturnType, expectedParamTypes);
}
@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;
}