概要:在JDeveloper 11gR1中,使用EJB, JPA and JavaServer Faces技术开发一个Web应用程序。Data Model,使用EJB diagrammer, EJB 3.0 and Java Persistence API (JPA)创建。View Controller,一个JSF Page会被创建用来浏览编辑HR信息。实例中,还创建了一个task flow用来查询查询员工,该task flow会做为ADF region添加到JSF Page上。
1. Part1:使用EJB3.0创建数据模型
可以使用EJB/JPA组件创建Web Application的Data Model。JDeveloper提供了wizards来创建EJB projects,实体,持久化单元,会话beans,消息驱动beans. Java Persistence API(JPA)提供了POJO持久化模型用来做面向对象的数据映射。
(1)创建一个新的Application和Projects
(2)创建持久化模型
使用EJB3.0实体beans创建Departments和Employees的持久化模型,该步完成后就会生成JPA实体。
(3)创建EJB Diagram
EJB Diagram可以可视化各种beans及其之间的关联关系。
(4)创建客户端应用程序使用的会话Bean
Session Bean是一些具体的业务逻辑,例如查找employees和departments的记录,就可以做成会话beans。Client端通过Session Bean层与EJB通讯。好处是:(1)减少了用户直接访问EJB3.0实体时的network traffic引起的性能问题;(2)是Client与EJB的business objects解耦合,代码容易维护。
有两种类型的session bean:Stateless session bean和 stateful session bean。stateful session bean使用场合:用户登录权限切片控制,购物车等。
A stateful session bean maintains conversational state on behalf of the client. A conversational state is defined as the session bean field values plus all objects reachable from the session bean fields. Stateful session beans do not directly represent data in a persistent data store, but they access and update data on behalf of the client. The lifetime of a stateful session bean is typically that of its client.
Stateless session beans are designed strictly to provide server-side behavior. They are anonymous because they contain no user-specific data. The EJB architecture provides ways for a single stateless session bean to serve the needs of many clients. All stateless session bean instances are equivalent when they are not involved in serving a client-invoked method. The term stateless means that it does not have any state information for a specific client. However, stateless session beans can have non-client specific state, for example, an open database connection.
Use Container for a transaction being handled by the session facade and bean for managing user transaction explicitly through a programmatic process.
(5)使用EJB会话beans创建ADF Data Controls.
2. Part2:创建View Project
Part1在EJB/JPA组件的基础上创建了Data Controls,下面创建JSF页面。
(1)为JavaServer Faces Page创建Global Layout
(2)将JSF Page绑定到Data Control
(3)运行测试页面
(4)增加新建/删除功能
3. Part3添加新的方法供UI使用
使用EJB3.0注解技术为实体Beans添加新的Data Control方法,然后将该方法暴漏给task flow中的Web Page。
(1)修为Employees实体Bean的@NamedQuery,为其添加通过工资查询员工的getEmployeesFindBySal()方法
(2)创建有两个JSF页面的Bounded Task Flow
【注意】有两种类型的task flow.
Unbounded task flow: A set of activities, control flow rules, and managed beans that interact to allow a user to complete a task. The unbounded task flow consists of all activities and control flows in an application, that are not included within a bounded task flow.
Bounded task flow: A specialized form of task flow that, in contrast to the unbounded task flow, has a single entry point (an entry point is a view activity that can be directly requested by a browser) and zero or more exit points. It contains its own set of private control flow rules, activities, and managed beans. A bounded task flow allows reuse, parameters, transaction management, reentry, and can render within an ADF region in a JSF page.
(3)将Task flow region添加到mainHR page
You can extract, configure, and package application functionality within a bounded task flow so that it can be added to other pages using an ADF region. ADF regions can be reused wherever needed, which means they are not dependent on a parent page.
(4)运行mainHr Page
4. Part4:Testing the Facade Inside and Outside the Java EE Container
Step 1: Add a New Method to the Entity and Expose it
Step 2: Run the Java Service outside the Java EE container
效果如下图所示:
参考
【1】http://docs.oracle.com/cd/E18941_01/tutorials/toc.htm
【2】http://st-curriculum.oracle.com/obe/jdev/obe11jdev/ps1/ejb/ejb.html
【3】http://blog.csdn.net/luyushuang/article/details/5885447
【4】http://hi.baidu.com/smilease/blog/item/798e42cbbfe7e4017f3e6fdb.html
【5】使用 EJB、JPA 和 JavaServer Faces 通过 JDeveloper 11g构建 Web 应用程序
http://www.oracle.com/ocom/groups/public/@otn/documents/webcontent/229517_zhs.html