Calling methods from JSF page doubts

Viewed 43929

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:

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

  2. 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 in BackBeanB. Why is that? Injected beans don't need to be initialized in @PostConstruct right? Are the getters and setters for the injected bean enough?

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

    enter image description here

    Why is that?

1 Answers
Related