Calling methods from JSF page doubts
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.
javing
Enthusiastic java developer based in London, I love stackoverflow, I use it regularly for many years and is a great way of helping and ask for help. Also i love blogging about software. Please visit my Blogs: Javing (Medium) Javing (Blogger)
Updated on July 07, 2020Comments
-
javing almost 4 years
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:
index.xhtml
<h:body> <!-- Using a method --> #{bba.salute()} <br/> <h:outputText value="#{bba.salute()}"/> <br/> <!-- Using a method from an injected bean--> #{bba.b.doSomething()} </h:body>
BackBeanA.java
@Named("bba") @SessionScoped public class BackBeanA implements Serializable { private static final long serialVersionUID = 5671761649767605303L; @Inject private BackBeanB b; public String salute() { return "Hi! I am 'A'"; } public BackBeanB getB() { return b; } public void setB(BackBeanB b) { this.b = b; } }
BackBeanB.java
@Named("bbb") @SessionScoped public class BackBeanB implements Serializable { private static final long serialVersionUID = -4786092545430477941L; public String doSomething() { System.out.println("Hello!!!"); return "I am a SessionScopped Backing Bean, my name is 'B' and i am doing something"; } }
This are the questions I have:
When I call a method from a backing bean, when do I need to use the brackets (), and when I don't need? Example: If I remove the brackets from
#{bba.salute()}
, I get an error, that says(Cannot find a property called 'salute')I also want to learn how to call a method from an injected bean. I injected BackBeanB, inside BackBeanA, but when I say
#{bba.salute()}
in the page, I don't see the message I from the method inBackBeanB
. Why is that? Injected beans don't need to be initialized in@PostConstruct
right? Are the getters and setters for the injected bean enough?Note the line where I say
<h:outputText value="#{bba.salute()}"/>
, it works, but eclipse displays a warning like this:Why is that?
-
javing almost 13 yearsIn point 2. Is it possible to access a method? I seei can access as property, but i am interested in calling it as a method, as in point 1. Is that possible? About point 3, Do you know how could i make eclipse EL syntax compatible?
-
BalusC almost 13 years1)
#{bean.getSalute()}
2) if you're not using Glassfish plugin / JBoss tools, simply turn off EL validation in Eclipse. -
OuuGiii about 6 yearsIs it always translated into getSalute()? or can it be translated into isSalute() if it is a boolean function?