使用ActionFunction标签可以很方便地实现Ajax。
通过这个标签类似于生成了一个JS方法,调用此JS方法去异步地调用Apex代码中的方法。
关键词:
action:引用的Controller中的方法名。
name:伪装成JavaScript时的方法名(在页面其他地方用此名称调用本方法)。
oncomplete:方法运行完毕后调用的JS方法名。
reRender:【重要】在运行完Ajax后,刷新相关的<apex:outputPanel>中的变量。
e.g:
public void saveAAA(){...your code here...}
<apex:actionFunction action="{!saveAAA}" name="saveBBB" reRender="newblock" oncomplete="afterAjax()" />
<apex:commandButton value="Save" onclick="saveBBB()" />
<apex:outputPanel id='newblock'>
<script type="text/javascript">
function afterAjax(){...your code here...}
</script>
</apex:outputPanel>
由于运行完Ajax后页面只有部分刷新,所以将'afterAjax()'方法写在<apex:outputPanel>里面,这样才能刷取到Controller中最新的值。
以上仅为个人见解,若有不足之处,欢迎指出。