javascript 在visualforce中从javascript调用控制器方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21047775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:54:57  来源:igfitidea点击:

Call a controller method from javascript in visualforce

javascriptsalesforcevisualforce

提问by Ramesh S

How can I call a controller method from VisualForce page without any onclick event happening? My VisualForce page would be like

如何在不发生任何 onclick 事件的情况下从 VisualForce 页面调用控制器方法?我的 VisualForce 页面会像

<apex:page standardController="Account"> 

    <script>
     /* Here i need to call a controller class with out any onclick event. It should load by itselef */

    </script>

</apex:page>

and my controller class will be like

我的控制器类会像

public class OrderPadController {
    /* this will be the constructor */

    public PageReference openPage() {
         PageReference newpage = new Pagereference('/apex'+'/pagetoopen'+'?aid='+a.id); 
         openorderpadembed.setRedirect(false); 
         return openorderpadembed;      
    }

From my VisualForce page I need to call the method openPage().

从我的 VisualForce 页面,我需要调用方法openPage()

Please help me out

请帮帮我

回答by Pavel Slepiankou

You can use JavaScript Remoting for Apex Controllersin the following manner:

您可以通过以下方式对 Apex 控制器使用JavaScript 远程处理:

<apex:page standardController="Account" extensions="OrderPadController">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      window.$j = jQuery.noConflict();
      $j( document ).ready(function() {
        OrderPadController.openPage(function(result, event){

            console.log(result);
            window.open(result,"_self");

        });
      });

    </script>
</apex:page>
public class OrderPadController {
    //

    @remoteAction
    public  PageReference openPage() {
        PageReference newpage = NEW Pagereference('/apex' + '/pagetoopen' + '?aid=' + a.id);
        openorderpadembed.setRedirect(false);
        return openorderpadembed;
    }
}

回答by eyescream

<apex:page standardController="Account" extensions="OrderPadController" 
    action="{!openPage}">
</apex:page>

the actionparameter will call the method you want as soon as the page loads. It is a dangerous attribute, one that will be pointed out to you if you'll go through Salesforce's security review process. The called action can manipulate database values for example (whereas the OrderPadController constructor can't) and the philosophy is that act of visiting a page shouldn't have any side effect.

动作参数会打电话给你想要的方法,只要在页面加载。这是一个危险的属性,如果您通过 Salesforce 的安全流程,就会向您指出该属性。例如,被调用的操作可以操作数据库值(而 OrderPadController 构造函数不能),其原理是访问页面的行为不应该有任何副作用。

In your specific scenario you can do it even without any Apex code at all:

在您的特定场景中,即使根本没有任何 Apex 代码,您也可以做到:

<apex:page standardController="Account" 
    action="/apex/Gantt?aid={!$CurrentPage.parameters.id}">
    Look Mom, no hands!
</apex:page>


As Pavel mentions - please write more about your requirement. I suspect that you don't even need this visualforce page acting as redirect, that a simple custom link or button (type = url instead of Visualforce or Javascript) could do the trick...

正如 Pavel 所提到的 - 请写更多关于您的要求。我怀疑你甚至不需要这个作为重定向的 Visualforce 页面,一个简单的自定义链接或按钮(type = url 而不是 Visualforce 或 Javascript)就可以做到这一点......