简体   繁体   English

在JSF中支持bean组织

[英]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:

  • View related stuff (save status, render logic, etc.). 查看相关内容(保存状态,渲染逻辑等)。 Stuff that is only needed in one request 仅在一个请求中需要的东西
  • Actions, action listeners, and value change listeners. 动作,动作侦听器和值更改侦听器。 If they are applicable to more than one view, they can be separated into their own file. 如果它们适用于多个视图,则可以将它们分成自己的文件。

Session scoped BackingBean: 会话作用域BackingBean:

  • Anything that needs to remain around longer than one request. 任何需要保持超过一个请求的东西。 Database data, SelectItems, etc. 数据库数据,SelectItems等
  • This bean is injected into the request bean, and stores instances of any data objects 此bean被注入请求bean,并存储任何数据对象的实例

Data Objects: 数据对象:

  • It doesn't seem to make sense to make the data objects into beans, so they are stored separately. 将数据对象设置为bean似乎没有意义,因此它们是分开存储的。 This would be things like User, Book, Car, any object that you are going to display on the page. 这将是用户,书籍,汽车,您将要在页面上显示的任何对象。 The object can contain view helper methods as well such as getFormattedName(), etc. 该对象也可以包含视图助手方法,例如getFormattedName()等。

DAO: DAO:

  • A non-bean object that handles all interaction with the business logic tier. 处理与业务逻辑层的所有交互的非bean对象。 It loads the data bean and prepares submits, etc. I usually make this a class of public static methods. 它加载数据bean并准备提交等。我通常将它作为一类公共静态方法。

Converters, Validators: 转换器,验证器:

  • Separate files 单独的文件

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: 我认为你一般都走在正确的轨道上,但是我会做一些不同的事情:

  1. 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视图。

  2. 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. 它还将您的业务逻辑与事件行为联系起来。

  3. 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创建一个接口,然后为此创建一个实现类。

  4. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM