自定义按钮或 Link 到带有标准控制器的 Visualforce 页面

Custom Button or Link to a Visualforce page with a standard controller

我使用视觉 force.How 创建页面来为标准页面创建自定义 button/link。我需要点击自定义button/link后打开页面。这该怎么做。我的代码如下

<apex:page standardcontroller="Account" tabstyle="Account" extensions="MyExtension" >
 <apex:form id="form1">
  <apex:commandlink action="{!showForm2}" value="Show the Form" rendered="{!showForm}" reRender="form2,op1"/>
</apex:form>
<apex:outputpanel id="op1">
 <apex:form id="form2">
 <apex:sectionheader title="Account Details" subtitle="{!if(Account.Id==null,'New Account',Account.Name)}"></apex:sectionheader>
<apex:pageblock mode="edit" id="leadPB" title="Account Edit">

 <apex:pageblockbuttons >
<apex:commandbutton action="{!save}" value="Save"></apex:commandbutton>
 <!-- If you wish to implement Save & New functionality you will have to write an Apex Extension with your own Save & New Method -->
 <apex:commandbutton action="{!cancel}" value="Cancel"></apex:commandbutton>
 </apex:pageblockbuttons>

        <apex:pageBlockSection >
            <apex:inputtext value="{!Account.LastName}" label="Customer Name"/>
            <apex:inputtext value="{!Account.PersonMobilePhone}"/>
            <apex:inputtext value="{!Account.CustomLandLine__c}"/>
            <apex:inputField value="{!Account.City__c}"/>
            <apex:inputField value="{!Account.PersonEmail}"/>
             <apex:inputField value="{!Account.Source__c}"/>

            <!-- <apex:commandButton action="{!save}" value="Save!"/>-->
        </apex:pageBlockSection>
    </apex:pageBlock>
<apex:pageMessages />
</apex:form>
</apex:outputpanel>
</apex:page>`

我的class代码

public with sharing class MyExtension {
    private ApexPages.StandardController sc;
    public MyExtension(ApexPages.StandardController sc) {
        this.sc = sc;
    }
    public PageReference save() {
        Account a = (Account) sc.getRecord();
        a.OwnerId = [select Id from User where LastName = 'Kapoor'].Id;
        a.OwnerId = [select Id from User where FirstName = 'Raoul'].Id;
        return sc.save();
    }
public boolean showForm{get;set;}

// default the var to false;
showForm = false;

public void showForm2(){
 showForm = true;
}
}

但是显示如下错误

控制器

Error: Compile Error: unexpected token: '=' at line 15 column 9

在我的页面中,我收到了这个错误

Unknown method 'AccountStandardController.showForm2()'

如何解决这个问题

请将 showForm = false; 放在 this.sc = sc; 的正下方。 constuctor 中的所有内容都用于 initialization/default 值。
您遇到第二个错误是因为控制器扩展未成功保存。修复第一个错误后,两个错误都应该消失。