2012-07-19 120 views
12

我有一個嵌入到Opportunities詳細信息頁面的Visualforce頁面。Visualforce頁面嵌入在需要重定向到其他頁面的詳細信息頁面中

頁面內有一個命令按鈕,用於調用後備控制器擴展中的方法。

一旦支持方法完成,我該如何將用戶重定向到另一個頁面?

我可以從該方法返回一個PageReference但它只會重定向嵌入Visualforce頁面顯示在iframe。

理想情況下,我想刷新頂層窗口,但我很擔心有如果嵌入式visualforce頁面與父窗口不在同一個域中,則可能會出現跨域問題。


正如我嘗試添加下列到嵌入式Visualforce頁面的基本測試:

<script> 
    window.setTimeout(testRedirect,2000); 
    function testRedirect() { 
     top.location.reload(); 
    } 
</script> 

這導致在Chrome中記錄錯誤:

Unsafe JavaScript attempt to access frame with URL https://na2.salesforce.com/006400000000000 from frame with URL https://ab2.na2.visual.force.com/servlet/servlet.Integration?lid=066400000000000&ic=1 . Domains, protocols and ports must match.

所以域的不同Visualforce頁面。

+1

我期待以此作爲我的[CSRF安全定製解決方案的一部分按鈕鏈接到Apex方法](http://stackoverflow.com/questions/10809520/csrf-safe-custom-button-linked-to-apex-method/11006021#11006021)的問題。這可能是我需要使用apex:actionFunction與一些JavaScript來完成改變'window.top.location.href'。 – 2012-07-19 00:58:12

+0

嗨,丹尼爾,除非有人有一些超級偷偷摸摸的策略,我認爲你會因此失去運氣 - 問題在於,嵌入式視覺頁面頁面是從不同的域提供的,所以你需要受到瀏覽器的XSS保護。希望有人證明我錯了,並找到一種方法來爲你做這個工作! – 2012-07-19 11:06:15

回答

13

這有點更多的代碼,但是這對我的作品在所有瀏覽器,我沒有得到任何類型的跨域錯誤。

控制器擴展:

public class Opp_Ext { 
    private ApexPages.StandardController stdController; 
    public String redirectUrl {public get; private set;} 
    public Boolean shouldRedirect {public get; private set;} 

    public Opp_Ext(ApexPages.StandardController stdController) { 
     this.stdController = stdController; 
     shouldRedirect = false; 
    } 

    public PageReference doStuffAndRedirect() { 
     shouldRedirect = true; 
     redirectUrl = stdController.view().getUrl(); 
     return null; 
    } 
} 

VF頁:

<apex:page standardController="Opportunity" extensions="Opp_Ext" > 
    <apex:form > 
     <apex:commandButton value="Do Stuff" action="{!doStuffAndRedirect}" rerender="redirectPanel" /> 
     <apex:outputPanel id="redirectPanel" > 
      <apex:outputText rendered="{!shouldRedirect}"> 
       <script type="text/javascript"> 
        window.top.location.href = '{!redirectUrl}'; 
       </script> 
      </apex:outputText> 
     </apex:outputPanel> 
    </apex:form> 
</apex:page> 
+0

太好了,謝謝。我想關鍵的一點是,跨域允許分配給window.top.location.href。然後這只是在按下按鈕之後呈現正確的Javascript的問題。我最終得到了類似於此的東西,但是在按下按鈕之後,只有JavaScript纔會發送到客戶端,這樣您的效果會更好。 – 2012-07-20 00:32:52

+0

我已經回過頭來,並提出了一個類似的解決方案,如果重定向到的URL是事先知道的。請參見[嵌入式Visualforce頁面頁面變爲內嵌](http://salesforce.stackexchange.com/questions/11158/pagereference-from-embedded-visualforce-page-becomes) – 2013-04-30 02:13:36

+0

太棒了!謝謝:) – 2014-09-10 10:23:54

0

嘗試使用PageReference

此外setRedirect將是有益的

樣品:

public class mySecondController { 
Account account; 

public Account getAccount() { 
    if(account == null) account = new Account(); 
    return account; 
} 

public PageReference save() { 
    // Add the account to the database. 

    insert account; 
    // Send the user to the detail page for the new account. 

    PageReference acctPage = new ApexPages.StandardController(account).view(); 
    acctPage.setRedirect(true); 
    return acctPage; 
} 

}

+2

我原本以爲是這樣,但它只是在VF頁面嵌入的迷你iframe中加載商機頁面佈局。 – JCD 2012-07-19 12:42:17

+0

將commandLink/commandButton的目標設置爲「_top」,並且它會將頁面加載到頂部幀,即完整的窗口。 – 2014-07-25 10:01:02

0

您需要使用oncomplete = "window.top.location.href = '{!'/'+obj.id}';"

+0

請提供更多解釋如何以及在哪裏使用。只有代碼纔會回答不滿,例如,通過閱讀答案的人無法快速計算出您的修補程序在做什麼或如何使用它。 – clearlight 2017-01-05 09:55:51

相關問題