开发者

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

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 Get开发者_开发知识库ter methods?

For example:

  1. <h:inputText value="#{BeanName.userName}"/>
  2. <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 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.


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


number 1 is correct from above it is the private field that you connect if you are using EL with JSF in your form.

You still need the getter and the setter which the managed bean calls to get the values so you can save them in a database ....etc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜