在<a4j:commandlink>标记</a4j:commandlink>中未调用action方法

时间:2012-08-23 14:03:13

标签: jsf-2 richfaces

<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>

问题是没有在辅助bean中调用deleteUser方法为什么会这样。但是它正在调用javascript函数请帮帮我。

1 个答案:

答案 0 :(得分:1)

问题是你在“onclick”方法中返回一个值。假设您的call js方法返回true或false,则代码必须更改为:

<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>

进一步解释:

实际代码的HTML生成代码将如下所示(或熟悉的东西):

<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... -->

如果您看到,return call();方法位于onclick的开头,则不会调用ajax提交。通过我提供的更新代码,代码将类似于:

<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... -->

通过此更改,如果您的call js方法返回false,则不会提交ajax调用,如果返回true,则将进行ajax调用。请注意,如果javascript方法没有返回任何值,默认情况下它将返回false。


更新:建议的代码适用于RichFaces 3.x.如果您使用RichFaces 4.x您的commandLink应该看起来像

<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>
相关问题