2

我需要记录从托管 Bean 触发的操作。此链接,在 PhaseListener 中记录调用的托管 bean 操作可帮助我解决与操作相关的问题。但是,当我使用 时actionListener,我有一个NullPointerException

@Override
public void beforePhase(PhaseEvent event) {
    FacesContext context = event.getFacesContext();

    if (context.isPostback()) {
        UICommand component = findInvokedCommandComponent(context);

        if (component != null) {
            String methodExpression = component.getActionExpression().getExpressionString(); 
            // It'll contain #{bean.action}.
        }
    }
}

private UICommand findInvokedCommandComponent(FacesContext context) {
    UIViewRoot view = context.getViewRoot();
    Map<String, String> params = context.getExternalContext().getRequestParameterMap();

    if (context.getPartialViewContext().isAjaxRequest()) {
        return (UICommand) view.findComponent(params.get("javax.faces.source"));
    } else {
        for (String clientId : params.keySet()) {
            UIComponent component = view.findComponent(clientId);

            if (component instanceof UICommand) {
                return (UICommand) component;
            }
        }
    }

    return null;
}

NullPointerException与线一起 出现

String methodExpression = component.getActionExpression().getExpressionString(); 

如何获取actionListener方法的名称?我试过了

private UICommand findInvokedCommandComponent(FacesContext context) {
        UIViewRoot view = context.getViewRoot();
        Map<String, String> params = context.getExternalContext().getRequestParameterMap();

        if (context.getPartialViewContext().isAjaxRequest()) {
            UIComponent component = view.findComponent(params.get("javax.faces.source"));
            if (component instanceof UICommand) {
//                component.get
              UICommand comp=  (UICommand) component;
                ActionListener[] actionListeners=  comp.getActionListeners();
                System.out.println("Taille des Listeners : "+actionListeners.length);
               ActionListener method;
                method = actionListeners[0];
                String toString = method.toString();
                System.out.println("ActionListener : "+toString);
                return (UICommand) component;
            }
        } else {
            for (String clientId : params.keySet()) {
                UIComponent component = view.findComponent(clientId);

                if (component instanceof UICommand) {
                    return (UICommand) component;
                }
            }
        }

        return null;
    }
System.out.println("ActionListener : "+toString); returns ActionListener : `javax.faces.event.MethodExpressionActionListener@16a779b` . What I would like to have is  `#{bean.action}` .Maybe I did it the wrong way
4

1 回答 1

4

你的方式很好,但你需要获得MethodExpressionActionListenerwhich 实现了ActionListener. 使用它,您仍然无法MethodExpression开箱即用,可能唯一的方法是通过反射获得它(不是最好的东西......)。

也就是说,您可以像这样修改代码:

if (component != null) {
    String methodExpression = "";

    if(component.getActionExpression() != null)
    {
        methodExpression = component.getActionExpression().getExpressionString(); 
    }
    else if(component.getActionListeners().length > 0)
    {
        methodExpression = getActionListener((MethodExpressionActionListener)component.getActionListeners()[0]).getExpressionString();
    }

    System.out.println("Method Expression : " + methodExpression);
}

您将需要此方法从以下内容中实际获取所需信息MethodExpressionActionListener

private MethodExpression getActionListener(MethodExpressionActionListener listener)
{
    MethodExpression expression = null;
    Field field;

    try
    {
        field = listener.getClass().getDeclaredField("methodExpressionZeroArg");
        field.setAccessible(true);
        expression = (MethodExpression)field.get(listener);

        if(expression == null)
        {
            field = listener.getClass().getDeclaredField("methodExpressionOneArg");
            field.setAccessible(true);
            expression = (MethodExpression)field.get(listener);
        }
    }
    catch(Exception e)
    {

    }

    return expression;
}
于 2013-06-23T22:47:23.827 回答