在PhaseListener中记录调用的托管Bean操作 [英] Logging the invoked managed bean action in a PhaseListener

查看:152
本文介绍了在PhaseListener中记录调用的托管Bean操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sun JSF 2.0,并编写了扩展javax.faces.event.PhaseListener的阶段侦听器.我能够记录源URI,目标URI,总时间等.但是到目前为止,尚无法记录该客户端事件期间将调用的ManagedBean和相应方法.我该怎么办?

I am using Sun JSF 2.0 and wrote a phase listener extending javax.faces.event.PhaseListener. I am able to log source URI, target URI, total time and so on. But so far unable to log the ManagedBean and corresponding method that would be invoked during that client event. How can I do this?

推荐答案

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

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. 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.

开球示例:

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

这篇关于在PhaseListener中记录调用的托管Bean操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆