Search code examples
javajsfrichfaces

Deprecated richfaces javax.faces.el.MethodBinding replacement use


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?

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

Solution

  • The javadocs state it clearly:

    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[]).

    Here's how to use it:

    MethodExpression methodExpression = 
        application.getExpressionFactory().createMethodExpression(
             FacesContext.getCurrentInstance().getELContext(), 
             "#{PrismBacking.onItemClick}", 
             null, 
             new Class[] { ActionEvent.class });
    menuItem.setActionExpression(methodExpression);