Logging the invoked managed bean action in a PhaseListener

前端 未结 1 1292
一向
一向 2021-01-06 03:58

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

相关标签:
1条回答
  • 2021-01-06 04:18

    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.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题