Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to bind input value to JSF managed bean property?

Tags:

jsf

el

I am new to JSF and managed beans. I have a managed bean with some private property with public setter and Getter methods. Now when I add the managed bean's properties to JSF forms, should I add the private methods directly or should I use call the property by Getter methods?

For example:

  1. <h:inputText value="#{BeanName.userName}"/>
  2. <h:inputText value="#{BeanName.getUserName()}"/>

Which one is correct in above?

like image 633
BlueBird Avatar asked Jun 24 '10 18:06

BlueBird


2 Answers

Assuming that you're using JBoss EL or EL 2.2+, both ways would work fine in the initial display. But the first one is actually more correct because the second one would only get the value, but never set the value. If you want to collect input values, you should always go for the first way. The EL (Expression Language) will then automatically locate the getUserName() and setUserName() methods whenever needed.

The second way will never work when you're using standard JSF EL implementation since it doesn't support direct method calls.

To learn more about JSF, start at our JSF wiki page.

like image 127
BalusC Avatar answered Sep 22 '22 09:09

BalusC


If in your java class you have something like

....
private String coolStuff;

public String getCoolStuff() {
    return coolStuff;
}
....

Then in your jsf page you access it like so:

#{myBackingBean.coolStuff}

The framework automatically looks for a method called getCoolStuff()

Hope that helps

like image 21
Java Drinker Avatar answered Sep 21 '22 09:09

Java Drinker