我希望在这篇文章中,我能够得到人们对于 JSF 页面和后台 bean 之间接口的最佳实践的意见。
One thing that I never can settle on is the structure of my backing beans. Furthermore, I have never found a good article on the subject.
哪些属性属于哪些后台 bean?什么时候应该向给定的 bean 添加更多的属性,而不是创建一个新的 bean 并将属性添加到它上面?对于简单的应用程序来说,考虑到将一个 bean 注入到另一个 bean 所涉及的复杂性,在整个页面中只有一个支持 bean 是否有意义?支持 bean 应该包含任何实际的业务逻辑,还是应该严格包含数据?
Feel free to answer these questions and any others that may come up.
As for reducing coupling between the JSF page and the backing bean, I never allow the JSF page to access any backing bean property's properties. For example, I never allow something such as:
<h:outputText value="#{myBean.anObject.anObjectProperty}" />
我总是需要这样的东西:
<h:outputText value="#{myBean.theObjectProperty}" />
支持 bean 值为:
public String getTheObjectProperty()
{
return anObject.getAnObjectProperty();
}
例如,当我在集合上循环时,我使用包装类来避免向下钻取到数据表中的对象。
In general, this approach feels "right" to me. It avoids any coupling between the view and the data. Please correct me if I'm wrong.