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:
<h:inputText value="#{BeanName.userName}"/><h:inputText value="#{BeanName.getUserName()}"/>
Which one is correct in above?
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
getthe value, but neversetthe value. If you want to collect input values, you should always go for the first way. The EL (Expression Language) will then automatically locate thegetUserName()andsetUserName()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.