jsf

How to override h:selectOneRadio renderer? Where is the renderer class in jsf-impl?


Is it possible to override renderer used by <h:selectOneRadio>? I tried to find the class from jsf-impl package of JSF 2.2 but didn't find it. The reason I want to do this is to get rid of the table it generates.


Solution

  • Is it possible to override renderer used by h:selectOneRadio?

    Yes, surely it is. Otherwise, UI component libraries like PrimeFaces couldn't exist.


    I tried to find the class from jsf-impl package but didn't find it.

    The exact class depends on the JSF implementation you're using. If it's Mojarra, then it's the com.sun.faces.renderkit.html_basic.RadioRenderer class. If it's MyFaces, then it's the org.apache.myfaces.renderkit.html.HtmlRadioRenderer class.

    In order to properly override it, just extend the class and override methods where necessary and register it as follows in your faces-config.xml:

    <render-kit>
        <renderer>
            <component-family>javax.faces.SelectOne</component-family>
            <renderer-type>javax.faces.Radio</renderer-type>
            <renderer-class>com.example.MyRadioRenderer</renderer-class>
        </renderer>
    </render-kit>
    

    Keep in mind that you're this way tight-coupling the renderer to the specific JSF impl/version. Such an extended renderer is not compatible with a different JSF implementation (i.e. your app wouldn't deploy when you ever replace Mojarra by MyFaces) and may possibly break when the current JSF implementation has been updated to a newer version. If you worry about this, consider writing the renderer entirely from scratch, like PrimeFaces et.al. do.


    The reason I want to do this is to get rid of the table it generates.

    Just upgrade to a minimum of JSF 2.3. See also <h:selectOneRadio> renders table element, how to avoid this?