14

我有一个嵌入在机会的详细信息页面上的 Visualforce 页面。

页面中有一个命令按钮,它调用支持控制器扩展中的方法。

支持方法完成后,如何将用户重定向到另一个页面?

我可以从该方法返回一个 PageReference,但它只会重定向显示嵌入式 Visualforce 页面的 iframe。

理想情况下,我想刷新顶级窗口,但我担心如果嵌入的 visualforce 页面与父窗口不在同一个域中,可能会出现跨域问题。


作为一个基本测试,我尝试将以下内容添加到嵌入式 Visualforce 页面:

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

这导致 Chrome 记录错误:

不安全的 JavaScript 尝试从 URL https://ab2.na2.visual.force.com/servlet/servlet.Integration?lid=066400000000000&ic=1 的框架访问具有 URL https://na2.salesforce.com/006400000000000框架 。域、协议和端口必须匹配。

因此,Visualforce 页面的域不同。

4

3 回答 3

15

这是更多的代码,但这在所有浏览器中都适用于我,而且我没有收到任何类型的跨域错误。

控制器扩展:

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>
于 2012-07-19T12:41:01.847 回答
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;
}

}

于 2012-07-19T12:31:32.670 回答
-1

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

于 2017-01-04T19:43:14.807 回答