问题
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?
回答1:
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;
}
来源:https://stackoverflow.com/questions/4788454/logging-the-invoked-managed-bean-action-in-a-phaselistener