2013-06-23 6 views
1

관리 Bean에서 실행 된 작업을 로깅해야합니다.이 링크 Logging the invoked managed bean action in a PhaseListener은 작업과 관련된 문제를 해결하는 데 도움이됩니다. 내가 actionListener를 사용할 때, 나는호출 된 관리 Bean actionListener를 PhaseListener에 로깅

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

가 어떻게이 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 선 발생이? 나는 당신은 좋은 방법에 있었다

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 : `[email protected]` . What I would like to have is `#{bean.action}` .Maybe I did it the wrong way 

답변

2

했지만, 당신은 ActionListener 구현하는 MethodExpressionActionListener을 얻을 필요가있다. 그것을 사용하면 여전히 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; 
} 
+0

덕분에 당신의 인내 :) 당신의 도움으로 해결 – bouikstefan

+0

@bouikstefan 그것은 당신의 질문에 적절하게 답변한다면 그것을 받아 들여야합니다! –