Bean Activated Activity Logging in PhaseListener

I am using Sun JSF 2.0 and have written a phase listener extending javax.faces.event.PhaseListener. I can register the source URI, destination URI, total time, and so on. But so far, it has not been possible to register the ManagedBean and the corresponding method that will be called during this client event. How can i do this?

+3
source share
1 answer

Input components send their client identifier as the name of the request parameter in the case of synchronous requests and as the value of the request parameter of the javax.faces.sourcerequest parameter in the case of asynchronous (ajax) requests. Just program the request parameters and check if UICommandcompnonent is allowed based UIViewRoot#findComponent()on this information and then process accordingly.

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;
}
+11
source

Source: https://habr.com/ru/post/1787588/


All Articles