Deprecated richfaces javax.faces.el.MethodBinding replacement use

前端 未结 3 1350
故里飘歌
故里飘歌 2020-12-19 11:01

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 in

相关标签:
3条回答
  • 2020-12-19 11:50

    The replacement mechanisms are detailed in the JEE5 API (of which JSF is part):

    • Application.createMethodBinding
    • Action.setActionListener
    0 讨论(0)
  • 2020-12-19 11:59

    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);
    
    0 讨论(0)
  • 2020-12-19 12:00

    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}");
    
    0 讨论(0)
提交回复
热议问题