不推荐使用richfaces javax.faces.el.MethodBinding替换使用

我发现这段代码的工作原理是我可以通过编程方式创建一个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 ); 

javadocs清楚地说明了这一点:

Application.createMethodBinding

不推荐 。 这已被替换为调用getExpressionFactory()然后调用ExpressionFactory.createMethodExpression(javax.el.E​​LContext,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); 

像往常一样,所有弃用确实只是在API文档中描述,包括有关替换的详细信息。

要有一个清晰的概述,这里有前JSF 1.2和后JSF 1.2方法动态创建Action和ActionListener:

在JSF 1.0 / 1.1中创建Action绑定:

 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)); 

为了避免大量的样板代码,你可以很好地将它包装在辅助方法中(如果需要在辅助/实用程序类中),例如:

 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}"); 

替换机制在JEE5 API(JSF的一部分)中有详细说明:

  • Application.createMethodBinding
  • Action.setActionListener