在ViewController中没有调用对象的方法(= ViewScoped ManagedBean)

时间:2014-02-20 16:40:28

标签: jsf managed-bean

简单问题...在commandLink的action属性中,我可以在viewController提供的对象上调用方法吗?我不能让以下设置工作,我不知道问题是什么 - 我没有得到错误,没有发生任何事情。

的TestObject

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

的ViewController

@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()也无济于事。

2 个答案:

答案 0 :(得分:0)

我认为问题是,action参数需要一个方法,而不是一个对象属性。 试着打电话:

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

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

答案 1 :(得分:0)

试试这个:

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