How does EL #{bean.id} call managed bean method bean.getId()
Asked Answered
L

2

1

I do not really understand how getter and setter work althougth it is a basic concept. I have the following code, how is the attribute id sent to Managed Bean? Is it captured by getter method?

My facelet

<p:inputText id="id" value="#{bean.id}">

My managed bean

private String id;

public void setId(String id) {
    this.id = id;
}

public String getId() {
      return id;
}
Linsk answered 20/11, 2014 at 16:11 Comment(1)
Possible duplicate of https://mcmap.net/q/16024/-why-use-getters-and-setters-accessorsAmeliaamelie
M
5

The call of getter and setter methods by #{} expressions is not part of JSF but Expression Language (most known as EL). JSF takes advantage of EL to bind the data of the HTML components to the fields of a bean through proper getters and setters. This is:

  • If the bean exists, Expression Language will execute the proper getter of the registered bean in the proper scope.
  • If client performs a form submission or an ajax request, then the components that are sent to the server (usually all the components in the <h:form>, in case of ajax requests you can state which components to send to the server) will contain a new value, and this value will be set to the field with the proper setter method.

For example, you have a SayHelloBean which belongs to request scope:

@RequestScoped
@ManagedBean
public class LoginBean {
    private String name;
    //proper getter
    public String getName() {
        return this.name;
    }
    //proper setter
    public void setName(String name) {
        this.name = name;
    }
}

And these 2 facelets pages (since it's an example I avoid declaring <html>, <h:head>, <h:body> and other elements, just focusing on the relevant code)

Page1.xhtml:

<h:form>
    Please tell me your name
    <h:inputText value="#{loginBean.name}" />
    <h:commandButton action="page2" />
</h:form>

Page2.xhtml:

Hello #{loginBean.name}

This is what happens behind the scenes:

  1. When Page1.xhtml is loaded, a new instance of LoginBean, which we may call loginBean, will be created by JSF and registered into JSP request scope. Since the value of <h:inputText /> is bound to LoginBean#name (which is read as the field name of LoginBean class), then EL will display the value of loginBean#name (which is read as the field name of instance loginBean), and since that is not initialized, EL will display null, as an empty string.

  2. When you submit the form of Page1.xhtml, since LoginBean is @RequestScoped then JSF will create a new instance of LoginBean, which we may call it loginBean2 (adding 2 in the end because this instance is totally different from the loginBean previously created) and will register it in JSP request scope. Since the value of <h:inputText /> is bound to LoginBean#name, JSF will validate and set the data by calling the proper setter. This will make loginBean2#name have the value of the <input type="text"> that was rendered by <h:inputText/>.

  3. At last, JSF will make sure to navigate to Page2.xhtml through forward, where when processing it, it will find #{loginBean.name} and EL will check for the value of loginBean2#name and replace it.

The steps explained here are a very small explanation (and with lot of elements not explained) of the JSF lifecycle and how JSF uses getters and setters.

More info:


Additional note: since you're learning JSF, avoid putting any business logic code in getters/setters. This is greatly explained here: Why JSF calls getters multiple times

Monopode answered 20/11, 2014 at 16:24 Comment(4)
Nicely explained. But how will I initialise a inputtext(mentioned in your case) with a value (setting default is a different matter, I am not asking that) from the database. If i do it (name initialising to someDataBaseValue()) with PostContruct, well it works then. But i failed to understand why this not works inside a contructor.Remex
@ShirgillAnsari while I did some debug on my managed bean constructors, I noticed that JSF didn't call them at all but the @PostConstruct method does (after injecting elements, if any). I don't know the exact details (and it's 1 am where I live) but I can do the research, and I believe BalusC has covered this topic on a blog post or here in StackOverflow. I'll let you know if I find info on the subject.Monopode
Thank you for your response. It's posted as a separate question. #27541073Remex
Sorry to disturb you. But even after considerable research effort, it yielded no fruitful result to me.Remex
B
1

Whenever you use something like

#{someBean.someField}

the EL looks for a someBean.getSomeField() or someBean.setSomeField(...) method, depending on whether you're reading that field or writing in it (which can easily be inferred from the context). JSF never accesses a field directly (i.e without making use of its getter or setter). Try deleting the getter and setter of a given field and you'll see it won't work.

Braziel answered 20/11, 2014 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.