Tuesday, April 27, 2010

Cannot call actionListener method on <a4j:commandLink/> with javascript

If you have problem with calling the actionListener method in JSF with 4j:commandLink and the javascript method for validation ...

javascript code ::

function confirmAdd(){

var flag=false;

if(confirm("Are you sure to add ??")){
flag=true;
}

}

and the JSF a4j:commandLink ::
<a4j:commandlink actionlistener="#{bean.addValues}" value="AddConfirm" onclick="return confirmAdd();" ajaxsingle="true">
<f:param value="#{bean.value}"/>
</a4j:commandLink>

then the method addValues in actionListener is not called ..

For that the Solution is ::

<a4j:commandlink actionlistener="#{bean.addValues}" value="AddConfirm" onclick="if(!confirmAdd()){return false;}" ajaxsingle="true">
<f:param value="#{bean.value}"/>
</a4j:commandLink>

then the method addValues in actionListener get called and the values are added.