Salesforce 单击“显示”按钮显示apex类代码

Salesforce 单击“显示”按钮显示apex类代码,salesforce,Salesforce,我有一个动态选择列表字段,其中包含组织中的所有apex类名称。页面上还有一个“显示”按钮。现在,如果用户从这个选择列表中选择一个值并单击Show按钮,那么该类的apex代码应该显示在下面。 请建议我如何在我的VF页面中实现它 谢谢 <apex:form > <apex:selectList value="{!selectedClass}" size="5"> <apex:selectOptions value="{!ClassList}" ></apex

我有一个动态选择列表字段,其中包含组织中的所有apex类名称。页面上还有一个“显示”按钮。现在,如果用户从这个选择列表中选择一个值并单击Show按钮,那么该类的apex代码应该显示在下面。 请建议我如何在我的VF页面中实现它

谢谢

<apex:form >
<apex:selectList value="{!selectedClass}" size="5">
<apex:selectOptions value="{!ClassList}" ></apex:selectOptions>
</apex:selectList>
<apex:pageBlock >
<apex:commandButton action="{!show}" value="Show" id="Button"/>
<apex:pageBlockSection title="My Current Class">

您可以查询
ApexClass
对象的
body
字段,以了解您要查找的内容:

public class SomeController  {

    private List<ApexClass> allApexClasses;
    public String selectedClass {public get; public set;}
    public String apexCodeOutput {public get; private set;}

    public SomeController() {
        // only select classes that aren't part of a managed package, since you won't be able to view the body
        allApexClasses = [select id, name, body from ApexClass where lengthwithoutcomments <> -1 order by name asc];
    }

    public List<SelectOption> getClassList() {
        List<SelectOption> opts = new List<SelectOption> opts;
        for ( ApexClass ac : allApexClasses )
            opts.add(new SelectOption(ac.Id, ac.Name));
        return opts;
    }

    public PageReference show() {
        if ( selectedClass != null ) {
            Id classId = (Id) selectedClass;
            for ( ApexClass ac : allApexClasses ) {
                if ( classId == ac.Id ) {
                    apexCodeOutput = ac.body;
                    break;
                }
            }
        }
        return null;
    }
}
公共类控制器{
私有列表AllapExclases;
公共字符串selectedClass{public get;public set;}
公共字符串apexCodeOutput{public get;private set;}
公共控制器(){
//只选择不属于托管包的类,因为您将无法查看主体
allApexClasses=[从纵向无注释的ApexClass中选择id、名称、正文-按名称asc排序1];
}
公共列表getClassList(){
列表选项=新列表选项;
用于(A类ac:A类)
选择添加(新选择选项(ac.Id,ac.Name));
返回选项;
}
公共页面参考显示(){
如果(selectedClass!=null){
Id classId=(Id)selectedClass;
用于(A类ac:A类){
如果(classId==ac.Id){
apexCodeOutput=ac.body;
打破
}
}
}
返回null;
}
}
然后在VF页面中,单击按钮时重新输入输出代码。您需要在代码周围使用
标记来保留间距,以便代码可读

<apex:form>
    <apex:selectList value="{!selectedClass}" size="5">
        <apex:selectOptions value="{!ClassList}" ></apex:selectOptions>
    </apex:selectList>
    <apex:pageBlock >
        <apex:commandButton action="{!show}" value="Show" rerender="apexoutput" id="Button"/>
        <apex:pageBlockSection title="My Current Class">
            <apex:outputPanel id="apexoutput">
                <pre>{!apexcodeoutput}</pre>
            </apex:outputPanel>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

{!apexcodeoutput}