jsf - 在 PhaseListener 中记录调用的托管 Bean 操作

标签 jsf logging jsf-2 phaselistener

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

最佳答案

输入组件在同步请求的情况下将其客户端 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;
}

关于jsf - 在 PhaseListener 中记录调用的托管 Bean 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4788454/

相关文章:

java - 如何通过托管 bean 显示 MySQL 数据?

JSF 从 HTTPS 重定向到 HTTP

tomcat - 已解决- Tomcat 9. 如何将 catalina.out 传递给服务日志 (journalctl)

azure - Azure Blob 存储是否适合存储许多(小型)通信日志?

java - Log4j2 初始化后配置 FileAppender

jsf-2 -/WEB-INF/lib 外部共享库 JAR 中的常见 Facelets 文件

jsf-2 - 当方法执行更新时刷新延迟加载的 Primefaces 数据表

javascript - 从 JavaScript + JSF 调用 Backing bean 方法

jsf - 如何使用JSF H :outputStylesheet?链接外部CSS资源

java - <f :metadata><f:viewParam> invoked 的 setter 到底是什么时候