【问题标题】:How can I pass variables state from one page to another, if both pages use the same controller, but the second page uses a controller extension?如果两个页面使用相同的控制器,但第二个页面使用控制器扩展,我如何将变量状态从一个页面传递到另一个页面?
【发布时间】:2013-04-02 01:18:27
【问题描述】:

我有一个页面需要将数据传递到另一个页面。它们都使用相同的控制器,但第二页有自己的控制器扩展。当第一页向第二页传递数据时,第二页会调用控制器的构造函数。如果我从第二页中删除控制器扩展,则可以传递数据并且构造函数只在第一页上调用,而不是在第二页上。

如果两个页面使用相同的控制器,但一个页面使用控制器扩展,我如何将状态从一个页面传递到另一个页面?

例如,在下面的代码中,如果我在 outputText 中为变量 t 输入数据,然后进入下一页,状态不会保存,构造函数会被调用,并且页面上 t 的值2 将是“构造”。如果 testExtension 从第二页中删除,那么一切正常。

顶点/test1 视觉力量

<apex:page controller="test">
    <apex:outputText value="{!t}" />
    <apex:commandButton action="/test2" value="Go to test2" />
</apex:page>

控制器

class test {
    public String t {get; set;}
    public test() {
        t = 'construct';
    }
}

apex/test2 VisaulForce

<apex:page controller="test" extension="testExtension">
    <apex:outputLabel value="{!t}" />
</apex:page>

控制器扩展:

class testExtension {
    public testExtension(test controller) {  
    }
}

【问题讨论】:

    标签: salesforce apex-code visualforce force.com


    【解决方案1】:
    public with sharing class Fake01_Controller {
    
    
        public String fake02String {get;set;}
    
    
        public PageReference fake03Submit() {
            PageReference prToNavigate = null;
    
            prToNavigate = Page.Fake_02;
    
            return prToNavigate;
        }
    
    }
    

    假01页:

    <apex:form >
    
        <apex:inputText value="{!fake02String}" />
    
        <apex:commandButton action="{!fake03Submit}" 
            value="Navigate to Fake 02" 
        />
    
    </apex:form>
    

    假02页:

    <apex:form >
    
        <apex:outputText value="{!fake02String}" />
    
    </apex:form>
    

    请阅读 PageReference 关于 setRedirect 方法:

    如果设置为 false,则重定向是服务器端转发,保留 当且仅当目标页面使用相同的控制器时,视图状态 并包含源页面使用的扩展的正确子集。

    【讨论】:

      【解决方案2】:
      class test {
          public String t {get; set;}
          public test() {
              t = 'construct';
          }
         public PageReference test2() {
             return page.test2;
          }
      }
      
      <apex:page controller="test">
          <apex:outputText value="{!t}" />
          <apex:commandButton action="{!test2)" value="Go to test2" />
      </apex:page>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-18
        相关资源
        最近更新 更多