且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 JSF 页面调用方法的疑惑

更新时间:2023-09-29 10:55:40

当你编写 #{myBean.salute} 时,JSF 正在寻找 property 敬礼.在 Java 代码中,它被翻译"为 myBean.getSalute();.换句话说,您必须为此属性提供 getter(如果此属性可以由 JSF 修改,则最终是 setter,例如,当它在输入字段中使用时).

When you write #{myBean.salute}, JSF is looking for the property salute. In Java code, it is "translated" to myBean.getSalute();. In others words, you have to provide the getter for this property (and eventually the setter if this property can be modified by JSF, when it is used in an input field for example).

当您编写 #{myBean.salute()} 时,您指的是 method salute().

When you write #{myBean.salute()} you are referring to the method salute().

规则很简单:当你想做一个动作时使用一个方法(即通常它会被定义在一个 actionactionListener 属性中).在其他情况下,使用属性.在您的示例中,您希望在页面中显示一些文本,因此调用 #{myBean.salute()},只需调用 #{myBean.salute}.

The rule is quite simple: use a method when you want to do an action (i.e. generally it will be defined inside an action or actionListener attribute). In the others cases, use a property. In your example, you want to display some text in your page, so instead calling #{myBean.salute()}, just call #{myBean.salute}.

对于第二点,尝试更改您的代码以访问属性 something 而不是方法:

For the second point, try to change your code to access the property something instead of the method:

<!-- Using a method from an injected bean-->
#{bba.b.something} 

和在 BeanB 代码中:

public String getSomething() {
    System.out.println("Hello!!!");
    return "I am a SessionScopped Backing Bean, my name is 'B' and i am doing something";
}

关于您的最后一点,我认为您的 Eclipse 根本无法处理 EL 2.0 语法.

Regarding your last point, I think that your Eclipse simply doesn't handle the EL 2.0 syntax.