Calling methods out of doubt in a JSF page

I have a couple of questions about how I call methods in EL. Maybe someone can explain how this works.

I made 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"; } } 

These are the questions that I have:

  • When I call a method from a backup bean, when do I need to use brackets (), and when do I not need it? Example: if I remove the brackets from #{bba.salute()} , I get an error message that says (cannot find a property called "salute")

  • I also want to learn how to call a method from the entered bean. I entered BackBeanB inside BackBeanA, but when I say #{bba.salute()} on the page, I canโ€™t see the message I am from the method in BackBeanB . Why is this? Insert beans do not need to be initialized in @PostConstruct to the right? Are getters and setters for the introduced bean enough?

  • Notice the line where I say <h:outputText value="#{bba.salute()}"/> , it works, but eclipse displays a warning similar to this:

    enter image description here

    Why is this?

+5
java java-ee el jsf jsf-2
Jul 6 '11 at 10:27
source share
1 answer

When you write #{myBean.salute} , the JSF looks for the salute property. In Java code, it is "translated" to myBean.getSalute(); . In other words, you must provide a getter for this property (and, ultimately, setter if this property can be changed by JSF when it is used, for example, in an input field).

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

The rule is quite simple: use the method when you want to take an action (i.e. usually it will be defined inside the action or actionListener ). In other cases, use the property. In your example, you want to display text on your page, so instead of calling #{myBean.salute()} just call #{myBean.salute} .

For the second point, try changing your code to access the something property 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"; } 

As for your last point, I think your Eclipse just doesn't handle EL 2.0 syntax.

+20
Jul 06 2018-11-11T00:
source share



All Articles