我发现这段代码的工作原理是我可以通过编程方式创建一个richfaces下拉菜单。但有些代码已被弃用。任何人都可以告诉我输入什么而不是弃用的电话吗?
由于
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();
}
}
}
已弃用的代码行是:
javax.faces.el.MethodBinding mb = app.createMethodBinding( "#{PrismBacking.onItemClick}", new Class[] { ActionEvent.class } );
menuItem.setActionListener( mb );
答案 0 :(得分:12)
像往常一样,所有弃用确实只是在API文档中描述,包括有关替换的详细信息。
要有一个清晰的概述,这里有前JSF 1.2和后JSF 1.2方法动态创建Action和ActionListener:
在JSF 1.0 / 1.1中创建动作绑定:
MethodBinding action = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.action}", new Class[0]);
uiCommandComponent.setAction(action);
在JSF 1.0 / 1.1中创建ActionListener绑定:
MethodBinding actionListener = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.actionListener}", new Class[] {ActionEvent.class});
uiCommandComponent.setActionListener(actionListener);
在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);
在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));
为了避免大量的样板代码,你可以很好地将它包装在辅助方法中(如果需要在helper / utility类中),例如:
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}));
}
以便您可以按如下方式使用它:
uiCommandComponent.setActionExpression(createAction("#{bean.action}", String.class);
uiCommandComponent.addActionListener(createActionListener("#{bean.actionListener}");
答案 1 :(得分:7)
javadocs清楚地说明了这一点:
Application.createMethodBinding
<强>已过时即可。这已被替换为调用getExpressionFactory()然后 ExpressionFactory.createMethodExpression(javax.el.ELContext,java.lang.String,java.lang.Class,java.lang.Class [])。
以下是如何使用它:
MethodExpression methodExpression =
application.getExpressionFactory().createMethodExpression(
FacesContext.getCurrentInstance().getELContext(),
"#{PrismBacking.onItemClick}",
null,
new Class[] { ActionEvent.class });
menuItem.setActionExpression(methodExpression);
答案 2 :(得分:1)
替换机制在JEE5 API(JSF的一部分)中有详细说明: