2

I would like to override the output of the default <f:ajax> rendering. Is there a possibility to override the handling of the <f:ajax> tag respectively the renderer of this tag?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sofarsoghood
  • 243
  • 2
  • 16
  • 1
    [What problem exactly are you trying to solve for which you possibly incorrectly thought that this all would be the right solution?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – BalusC May 29 '15 at 09:43
  • As you can see by the comment above, short is not always good ;-) – Kukeltje May 29 '15 at 09:52
  • Thanks for your fast response !i have several tags in my xhtml sourcecode. When rendered to the frontend i want to avoid inline javascript. I just want to put it into a – sofarsoghood May 29 '15 at 09:58

1 Answers1

2

You can override the <f:ajax> renderer by creating a custom ClientBehaviorRenderer implementation and register it as <client-behavior-renderer> in faces-config.xml on renderer type javax.faces.behavior.Ajax.

public class YourAjaxBehaviorRenderer extends ClientBehaviorRenderer {

    @Override
    public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
        return "alert('Put your JS code here.')";
    }

}
<render-kit>
    <client-behavior-renderer>
        <client-behavior-renderer-type>javax.faces.behavior.Ajax</client-behavior-renderer-type>
        <client-behavior-renderer-class>com.example.YourAjaxBehaviorRenderer</client-behavior-renderer-class>
    </client-behavior-renderer>
</render-kit>

Do note that there's a @FacesBehaviorRenderer annotation anologous to @FacesRenderer, but when there's already a specific renderer type registered in any faces-config.xml, it would always have precedence over the annotation. Since this is already provided by JSF impl itself, the annotation becomes useless.

For reference, Mojarra uses com.sun.faces.renderkit.html_basic.AjaxBehaviorRenderer and MyFaces uses org.apache.myfaces.renderkit.html.HtmlAjaxBehaviorRenderer for this purpose.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555