【问题标题】:Method not called on object in ViewController ( = ViewScoped ManagedBean)未在 ViewController 中的对象上调用方法(= ViewScoped ManagedBean)
【发布时间】:2014-02-20 16:40:27
【问题描述】:

简单的问题......在commandLink 的action 属性中,我可以调用viewController 提供的对象的方法吗?我无法使以下设置正常工作,而且我不知道问题出在哪里 - 我没有收到错误,只是没有发生任何事情。

测试对象

public class TestObject {
    public void testMethod() {
        System.out.println("This is a test method");
    }
}

视图控制器

@ManagedBean
@ViewScoped
public class ViewController {

    private TestObject testObject = new TestObject();

    public TestObject getTestObject() {
        return testObject;
    }

    public void testMethod() {
        testObject.testMethod();
    }
}

JSF

<!-- This does not work -->
<h:commandLink action="#{viewController.testObject.testMethod}" value="On internal Object"/>
<!-- This works -->
<h:commandLink action="#{viewController.testMethod}" value="On ViewController"/>

编辑:抱歉,忘了提及:我尝试拨打 testMethod()viewController.getTestObject().testMethod() 均无济于事。

【问题讨论】:

    标签: jsf managed-bean


    【解决方案1】:

    试试这个:

    <h:commandLink action="#{viewController.testObject.testMethod()}" value="On internal Object"/>
    

    【讨论】:

      【解决方案2】:

      我认为问题在于,action 参数需要一个方法,而不是对象属性。 尝试调用:

      <h:commandLink action="#{viewController.testObject.testMethod()}" value="On internal Object"/>
      

      然后收到来自viewController 的属性testObject 并调用testMethod()

      【讨论】:

      • TestObject#testMethod 是一种方法,而不是 bean 属性
      • 是的。我无法解释你的评论,你的意思是什么?我是否另有说明?
      • TestObject#testMethod 表示类 testMethod 上的方法 testMethod。你如何证明action="#{viewController.testObject.testMethod}" 调用viewController#testObject()#testMethod()(因此失败)而不是viewController#getTestObject()#testMethod()
      • 您可以根据需要逐步完成评估进度。我正在改写我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2013-06-13
      • 2011-12-07
      • 2012-12-22
      • 2018-11-17
      • 1970-01-01
      • 2011-11-02
      相关资源
      最近更新 更多