在PhaseListener中记录调用的托管bean操作

Hus*_*ain 4 jsf logging jsf-2 phaselistener

我正在使用Sun JSF 2.0并编写了一个阶段监听器扩展javax.faces.event.PhaseListener.我能够记录源URI,目标URI,总时间等.但到目前为止无法记录ManagedBean以及在该客户端事件期间将调用的相应方法.我怎样才能做到这一点?

Bal*_*usC 12

在同步请求的情况下,输入组件将其客户端ID作为请求参数名称javax.faces.source发送,在异步(ajax)请求的情况下,作为请求参数的请求参数值发送.只需遍历请求参数并根据此信息检查组件是否可UICommand解析UIViewRoot#findComponent(),然后相应地处理.

开球示例:

@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;
}
Run Code Online (Sandbox Code Playgroud)