Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

java - 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 );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As usual, all the deprecation is indeed just described in the API docs, including details about the replacement.

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:

Create Action binding in JSF 1.0/1.1:

MethodBinding action = FacesContext.getCurrentInstance().getApplication()
    .createMethodBinding("#{bean.action}", new Class[0]);
uiCommandComponent.setAction(action);

Create ActionListener binding in JSF 1.0/1.1:

MethodBinding actionListener =  FacesContext.getCurrentInstance().getApplication()
    .createMethodBinding("#{bean.actionListener}", new Class[] {ActionEvent.class});
uiCommandComponent.setActionListener(actionListener);

Create Action expression in JSF 1.2 or newer:

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...