简体   繁体   English

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

[英]Logging the invoked managed bean action in a PhaseListener

I am using Sun JSF 2.0 and wrote a phase listener extending javax.faces.event.PhaseListener . 我正在使用Sun JSF 2.0并编写了一个扩展javax.faces.event.PhaseListener的阶段监听器。 I am able to log source URI, target URI, total time and so on. 我能够记录源URI,目标URI,总时间等。 But so far unable to log the ManagedBean and corresponding method that would be invoked during that client event. 但到目前为止无法记录ManagedBean以及在该客户端事件期间将调用的相应方法。 How can I do this? 我怎样才能做到这一点?

Input components send their client ID as request parameter name in case of synchronous requests and as request parameter value of javax.faces.source request parameter in case of asynchronous (ajax) requests. 在同步请求的情况下,输入组件将其客户机ID作为请求参数名称发送,在异步(ajax)请求的情况下,作为请求参数的javax.faces.source请求参数值。 Just loop through the request parameters and check if an UICommand compnonent is resolveable by UIViewRoot#findComponent() based on this information and then handle accordingly. 只需循环遍历请求参数,并根据此信息检查UICommand UIViewRoot#findComponent()是否可以UICommand UICommand组件,然后进行相应处理。

Kickoff example: 开球示例:

@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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM