虽然 mmix 是正确的,但实际上有一种方法可以模拟这一点,它只需要一点创造性地使用 apex:actionFunction 和一些 javascript。您基本上将操作分解为两个调用来处理 http 的客户端/服务器模型的现实。
首先是验证调用,以确定是否应向用户显示弹出窗口,其次是根据用户响应在需要时调用保存。请记住,我的解决方案取决于 javascript 确认框,所以这里没有花哨的 ui :)
仅供参考 - 我在我的示例中确实引用了 jquery,只需将 jquery 最小化库保存为 jquery.txt 并使用名称“jquery”添加到您的静态资源中,您就可以开始使用了。
使用以下代码创建一个控制器类:
public class ActionFunctionCLS {
public Boolean causeValidationWarning {get; set;} // Just used for controlling the test behavoir from the ui. Not a part of the solution.
public String validationWarning {get; set;}
// Validates the record (or whatever) is good. If it is then we call save, else we set a warning message that
// tells the ui to display a javascript popup box to the user confirming they really want to proceed.
public PageReference ValidateAndSaveRecord() {
PageReference result = null;
if(causeValidationWarning){
validationWarning = 'This is a validation warning, hit Ok if you\'re sure you want to proceed.';
}
else {
validationWarning = '';
result = SaveRecord();
}
return result;
}
// Clears the validation warning so the ui won't try and display a confirmation box to the user
public void clearvalidationWarning(){
validationWarning = '';
}
// Actually saves the record and then performs some post save function. Could be anything from displaying a save message (which means
// we'd need to clear the validation warning to keep the confimation pop from appearing), or it could even be a redirect somewhere else.
public PageReference SaveRecord() {
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Save Complete!', 'And I totally could have redirected you or something.');
ApexPages.addMessage(myMsg);
clearvalidationWarning();
return null;
}
// Just to prove that our Validation & Save logic won't affect other functionality
public PageReference DoSomething() {
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'I did some other work.');
ApexPages.addMessage(myMsg);
return null;
}
}
然后创建一个视觉力页面:
<apex:page controller="ActionFunctionCLS" id="pg" showHeader="true" sidebar="true">
<apex:includeScript value="{!$Resource.jquery}"/>
<script>
var j$ = jQuery.noConflict(); //Needed for jQuery usage
j$(document).ready(function(){
var validationWarningMsg = "{!validationWarning}";
if(validationWarningMsg != ''){ // There was a validation warning, make sure the user still wants to save
if(confirm(validationWarningMsg)){
saveRecordAction();
}
else {
clearvalidationWarningAction();
}
}
});
</script>
<apex:form id="myForm">
<apex:pageMessages />
<apex:actionfunction name="saveRecordAction" action="{!SaveRecord}" />
<apex:actionfunction name="clearvalidationWarningAction" action="{!clearvalidationWarning}" />
<apex:pageBlock tabStyle="Account">
<apex:pageBlockButtons >
<apex:commandButton value="Save Record" action="{!ValidateAndSaveRecord}" />
<apex:commandButton value="Do Other Work" action="{!DoSomething}" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Stuff" columns="1" >
<apex:inputCheckbox value="{!causeValidationWarning}" label="Cause a validation warning" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
并测试!apex:actionFunction 是我最喜欢的新功能之一,它允许您从 javascript 直接对控制器方法进行 ajax 调用,而无需编写丑陋的代码,非常简洁。
这里发生的事情是保存按钮调用了 ValidateAndSaveRecord(),它运行我们想象的验证,然后如果一切看起来都很好,则实际保存。如果不是,它会在控制器上设置一个字符串属性并返回。页面上的 javascript 在页面加载时查找此字符串,如果找到,则会显示确认弹出窗口。
当用户单击“确定”时,另一个 ajax 调用将被发送到 Save() 但跳过验证,因为这已经完成了。如果他们单击取消,仍会调用以清除警告字符串,因此下一次回发不会导致页面再次显示弹出窗口。这一步非常重要!
我认为我的解决方案可能会引起一些胃痛,因为它非常依赖于取消调用,但根据我的经验,它非常可靠。享受。