雖然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調用將被保存(),但會跳過驗證,因爲這已經完成。如果他們點擊取消,仍然會進行呼叫以清除警告字符串,以便下一次回發不會導致頁面再次顯示彈出式窗口。 這一步非常重要!
我認爲我的解決方案可能會導致一些胃灼熱,因爲它取決於那麼重的取消呼叫,但是在我的經驗中它非常可靠。請享用。
嗨mmix!非常感謝您的好評。玩得開心! – Channa