Search code examples
jsf-2richfaces

action method is not called in <a4j:commandLink> tag


<a4j:commandLink onclick="return call();" action="#{bean.deleteUser(all_user.userID)}" reRender="viewUserGrid">
<h:graphicImage style="border-style:none;" url="/images/delete.jpg"  height="10px" />
</a4j:commandLink>

The problem is deleteUser method is not getting invoked in the backing bean why is it so.But it is invoking the javascript function Please help me.


Solution

  • The problem is that you're returning a value in your "onclick" method. Assumming that your call js method returns a true or false, the code must be changed to:

    <a4j:commandLink onclick="if (!call()) return false;"
        action="#{bean.deleteUser(all_user.userID)}"
        reRender="viewUserGrid" limitToList="true">
        <h:graphicImage style="border-style:none;" url="/images/delete.jpg"  height="10px" />
    </a4j:commandLink>
    

    Further explanation:

    The HTML generated code for your actual code will look like this (or something familiar):

    <a href="#" id="formName:j_id45351"
        name="formName:j_id22"
        onclick="return call(); A4J.AJAX.Submit('formName',event, '');">
    <!-- the rest of the HTML generated code... -->
    

    If you see, the return call(); method is at the beginning of the onclick, so the ajax submit won't be called. By the code updated I provide, the code will be similar to this:

    <a href="#" id="formName:j_id45351"
        name="formName:j_id22"
        onclick="if (!call()) return false; A4J.AJAX.Submit('formName',event, '');">
    <!-- the rest of the HTML generated code... -->
    

    With this change, if your call js method returns false, then the ajax call won't be submitted, if it returns true, then your ajax call will be made. As a note, if a javascript method doesn't return any value, it will return false by default.


    UPDATE: The proposed code will work with RichFaces 3.x. In case you use RichFaces 4.x Your commandLink should look like

    <a4j:commandLink onclick="if (!call()) return false;"
        action="#{bean.deleteUser(all_user.userID)}"
        render="viewUserGrid">
        <h:graphicImage style="border-style:none;" url="/images/delete.jpg"
            height="10px" />
    </a4j:commandLink>