How to change the standard href = "#" attribute of h: commandLink?

I support AJAX JSF2 application, and we are committed to using tags h:commandLinksand f:ajaxfor all actions - always reversing what you need.

This, of course, violates the expected behavior of the user when correctly clicking on the links and selecting "Open the link in a new tab", etc.

I understand that f: ajax forces the href attribute of the resulting element to abe #and performs the entire magic attack of the request in the onclick function - now I want to provide auxiliary support for "Open Link ..." by adding some meaningful link to the href attribute of the resulting tag <a>.

This will not violate the “normal” behavior of onclick, since the generated javascript always ends with return false;, but will allow me to send my users to a page using a regular GET request if they want to open the link in a new window.

Is there any way to do this? Or can someone point me in the right direction, where in the JSF life cycle will I have to jump in to do this, possibly using a phase listener?

+5
source share
1 answer

The easiest way would be to expand com.sun.faces.renderkit.html_basic.CommandLinkRendererand redefine renderAsActive(). Mojarra is open source, just copy the method and edit the line it says writer.write("href", "#", "href"). Replace the string "#"according to your understanding.

public class MyCommandLinkRenderer extends CommandLinkRenderer {

    @Override
    protected void renderAsActive(FacesContext context, UIComponent command) throws IOException {
        // ...
    }

}

, faces-config.xml:

<render-kit>
    <renderer>
        <component-family>javax.faces.Command</component-family>
        <renderer-type>javax.faces.Link</renderer-type>
        <renderer-class>com.example.MyCommandLinkRenderer</renderer-class>
    </renderer>
</render-kit>

, Mojarra. JSF-, Mojarra.


, h: outputLink h: commandLink?

+2

All Articles