[英]Backing bean organization in JSF
I have been thinking about this for a while now and have yet to come up with a best practice for how to organize my beans/classes in a JSF project for the presentation tier. 我一直在考虑这个问题,并且还没有提出如何在表示层的JSF项目中组织我的bean /类的最佳实践。 Obviously there are many factors that come into play, but I would like to discuss.
显然有很多因素可以发挥作用,但我想讨论一下。 Here is my current line of thought:
这是我目前的思路:
Consider a basic JSF (still stuck on JSF 1.xx here unfortunately) application that contains a view page (view the data) and an edit page (add, update, delete the data). 考虑一个基本的JSF(在这里仍然坚持JSF 1.xx)应用程序包含一个视图页面(查看数据)和一个编辑页面(添加,更新,删除数据)。 Here is how I would organize the project:
以下是我组织项目的方法:
Request scoped BackingBean: 请求作用域BackingBean:
Session scoped BackingBean: 会话作用域BackingBean:
Data Objects: 数据对象:
DAO: DAO:
Converters, Validators: 转换器,验证器:
This seems to be all that is needed in your average JSF application. 这似乎是普通JSF应用程序所需要的全部内容。 I have read through this: http://java.dzone.com/articles/making-distinctions-between , as well the replies here: JSF backing bean structure (best practices) , but I never felt like we got a complete picture.
我已经读完了这个: http : //java.dzone.com/articles/making-distinctions-between ,以及这里的回复: JSF支持bean结构(最佳实践) ,但我从未觉得我们得到了完整的图片。 BalusC's response was helpful, but didn't seem to quite cover a full app.
BalusC的回复很有帮助,但似乎没有完全涵盖完整的应用程序。 Let me know your thoughts!
让我知道你的想法!
I think you are on the right track generally, however I would do a few things differently: 我认为你一般都走在正确的轨道上,但是我会做一些不同的事情:
I would take your DAO layer and split it into two seperate layers, one pure DAO layer that merely is responsible from fetching data from various sources (Eg. database calls, web service calls, file reads, etc...). 我会把你的DAO层分成两个单独的层,一个纯DAO层,只负责从各种来源获取数据(例如数据库调用,Web服务调用,文件读取等等)。 I would then have a Business Logic layer that contains passthroughs to DAO calls as well as any additional calculations, algorithms, or other general business logic that isn't specific to any one JSF view.
然后,我将拥有一个业务逻辑层,其中包含对DAO调用的直通以及任何其他计算,算法或其他一般业务逻辑,这些逻辑并非特定于任何一个JSF视图。
In the MVC pattern, your ManagedBean plays the role of the Controller, and as such should also be the repository for Presentation Logic (logic that is specific to manipulating the view or interacting between various View components). 在MVC模式中,ManagedBean扮演Controller的角色,因此也应该是Presentation Logic的存储库(特定于操纵视图或在各种View组件之间交互的逻辑)。 It will also tie your business logic into the event behavior as well.
它还将您的业务逻辑与事件行为联系起来。
I would not use public static methods for your Business Logic or DAO layer. 我不会为您的业务逻辑或DAO层使用公共静态方法。 This doesn't lend itself well to automated unit tests and prevents your app from utilizing Dependency Injection frameworks like CDI or Spring.
这不适合自动化单元测试,并阻止您的应用程序使用CDI或Spring等依赖注入框架。 Instead create an interface for your BO's and DAO's and then an implementation class for this.
而是为您的BO和DAO创建一个接口,然后为此创建一个实现类。
On that note, utilize a Dependency Injection Framework like CDI or Spring :) It will allow you to automatically inject Business Logic objects or DAO's into your ManagedBeans as well as your unit tests. 在这方面,使用像CDI或Spring这样的依赖注入框架:)它将允许您自动将业务逻辑对象或DAO注入到ManagedBeans以及单元测试中。 It will also allow for you to swap implementations or DAO's without any coupling to code in other layers of your application.
它还允许您交换实现或DAO,而无需与应用程序的其他层中的代码进行任何耦合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.