java 弃用的 Richfaces javax.faces.el.MethodBinding 替换使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2158009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deprecated richfaces javax.faces.el.MethodBinding replacement use
提问by volvox
I found this piece of code works in that i can programmatically creates a richfaces dropdown menu. But some of the code is deprecated. Can anyone tell me what to put in instead of the deprecated calls?
我发现这段代码有效,因为我可以以编程方式创建一个 Richfaces 下拉菜单。但有些代码已被弃用。谁能告诉我用什么代替已弃用的调用?
Thanks
谢谢
public HtmlDropDownMenu getMyMenu()
{
HtmlDropDownMenu menu = new HtmlDropDownMenu();
menu.setValue( "Node Select" );
HtmlMenuItem menuItem = new HtmlMenuItem();
// TODO programmatically pass from getNodes into a String[] rather than an ArrayList of SelectItems
String subOption = "myBox";
menuItem.setValue( subOption );
Application app = FacesContext.getCurrentInstance().getApplication();
javax.faces.el.MethodBinding mb = app.createMethodBinding( "#{PrismBacking.onItemClick}", new Class[] { ActionEvent.class } );
menuItem.setActionListener( mb );
menu.getChildren().add( menuItem );
return( menu );
}
public void onItemClick( ActionEvent event )
{
Object obj = event.getSource();
if( obj instanceof HtmlMenuItem )
{
HtmlMenuItem item = (HtmlMenuItem)obj;
if( item != null )
{
lastItem = item.getValue().toString();
}
}
}
deprecated code lines are:
弃用的代码行是:
javax.faces.el.MethodBinding mb = app.createMethodBinding( "#{PrismBacking.onItemClick}", new Class[] { ActionEvent.class } );
menuItem.setActionListener( mb );
回答by BalusC
As usual, all the deprecation is indeed just described in the API docs, including details about the replacement.
像往常一样,所有的弃用确实只是在 API 文档中进行了描述,包括有关替换的详细信息。
To have a clear overview, here are both the pre-JSF 1.2 and post-JSF 1.2 ways to create an Action and ActionListener dynamically:
为了有一个清晰的概述,以下是 JSF 1.2 之前和 JSF 1.2 之后的动态创建 Action 和 ActionListener 的方法:
Create Action binding in JSF 1.0/1.1:
在 JSF 1.0/1.1 中创建 Action 绑定:
MethodBinding action = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.action}", new Class[0]);
uiCommandComponent.setAction(action);
Create ActionListener binding in JSF 1.0/1.1:
在 JSF 1.0/1.1 中创建 ActionListener 绑定:
MethodBinding actionListener = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.actionListener}", new Class[] {ActionEvent.class});
uiCommandComponent.setActionListener(actionListener);
Create Action expression in JSF 1.2 or newer:
在 JSF 1.2 或更新版本中创建 Action 表达式:
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression action = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.action}", String.class, new Class[0]);
uiCommandComponent.setActionExpression(action);
Create ActionListener expression in JSF 1.2 or newer:
在 JSF 1.2 或更新版本中创建 ActionListener 表达式:
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression actionListener = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.actionListener}", null, new Class[] {ActionEvent.class});
uiCommandComponent.addActionListener(new MethodExpressionActionListener(actionListener));
To avoid lot of boilerplate code, you can just wrap it nicely in helper methods (if necessary in an helper/utility class), e.g.:
为了避免大量样板代码,您可以将它很好地包装在辅助方法中(如果需要在辅助/实用程序类中),例如:
public static MethodExpression createAction(String actionExpression, Class<?> returnType) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), actionExpression, returnType, new Class[0]);
}
public static MethodExpressionActionListener createActionListener(String actionListenerExpression) {
FacesContext context = FacesContext.getCurrentInstance();
return new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), actionListenerExpression, null, new Class[] {ActionEvent.class}));
}
so that you can just use it as follows:
这样您就可以按如下方式使用它:
uiCommandComponent.setActionExpression(createAction("#{bean.action}", String.class);
uiCommandComponent.addActionListener(createActionListener("#{bean.actionListener}");
回答by Bozho
The javadocs state it clearly:
javadocs 清楚地说明了这一点:
Application.createMethodBinding
Application.createMethodBinding
Deprecated. This has been replaced by calling getExpressionFactory() then ExpressionFactory.createMethodExpression(javax.el.ELContext, java.lang.String, java.lang.Class, java.lang.Class[]).
已弃用。这已被替换为调用 getExpressionFactory() 然后ExpressionFactory.createMethodExpression(javax.el.ELContext, java.lang.String, java.lang.Class, java.lang.Class[])。
Here's how to use it:
以下是如何使用它:
MethodExpression methodExpression =
application.getExpressionFactory().createMethodExpression(
FacesContext.getCurrentInstance().getELContext(),
"#{PrismBacking.onItemClick}",
null,
new Class[] { ActionEvent.class });
menuItem.setActionExpression(methodExpression);
回答by McDowell
The replacement mechanisms are detailed in the JEE5 API (of which JSF is part):
替换机制在 JEE5 API(JSF 是其中的一部分)中有详细说明:

