Calling methods from JSF page doubts

后端 未结 1 1480
庸人自扰
庸人自扰 2020-12-15 12:41

I have a couple of questions about the way I call methods in EL. Maybe someone could explain how it actually works.

I did this very simple example:

i

相关标签:
1条回答
  • 2020-12-15 13:19

    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).

    When you write #{myBean.salute()} you are referring to the method 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}.

    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} 
    

    and in BeanB code:

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

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

    0 讨论(0)
提交回复
热议问题