Phát triển ứng dụng mã nguồn mở - Bài 6: Java web application jsf – java ee
• JSF ─ Java Server Face • Entity Classes from Database Wizard • JSF Pages from Entity Classes Wizard • JSF Web application CRUD with MySQL Database
Bạn đang xem trước 20 trang tài liệu Phát triển ứng dụng mã nguồn mở - Bài 6: Java web application jsf – java ee, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Đoàn Thiện Ngân Bài 6 - 1/22
Bài 6:
Java Web Application
JSF – Java EE
GV: ĐOÀN THIỆN NGÂN
Đoàn Thiện Ngân Bài 6 - 2/22
Nội dung
• JSF ─ Java Server Face
• Entity Classes from Database Wizard
• JSF Pages from Entity Classes Wizard
• JSF Web application CRUD with
MySQL Database
Đoàn Thiện Ngân Bài 6 - 3/22
Tài liệu tham khảo
1. Java™ For Programmers, 2nd Edition,
Deitel® Developer Series, Chapters 26 & 27
2. NetBeans IDE 7 Cookbook, 2011, Packt
Publishing
3. Oracle Java EE 7 Tutorial
/jsf-intro.htm
4. Netbeans Tutorial,
https://netbeans.org/kb/docs/web/jsf20-
intro.html
Đoàn Thiện Ngân Bài 6 - 4/22
JSF Web Application
• JSP was great in its time. But as with everything, time
passed and other technologies were developed that
made it even easier to generate this same kind of
content, and on top of that, overcame many of the
limitations that were introduced with JSP.
• Enter JSF. It brings a graphical development mentality,
similar to Swing's, which relies on a set of reusable UI
components and event-driven approach.
• Instead of totally breaking away from its roots, JSF 2.0
can also be developed in the same way JSP is; in fact, a
JSF application is a JSP/servlets application.
• Typically, Web Applications are developed using the
MVC design pattern. This pattern works great with JSF
since, in a typical JSF application, the View is the page
we write, the Model is the data we wish to show, and the
Controller is the FacesServlet. Every request passes
through the FacesServlet. The configuration file for the
FacesServlet is faces-config.xml.
Đoàn Thiện Ngân Bài 6 - 5/22
What is Java EE?
• Java EE (Enterprise Edition) is a widely
used platform containing a set of
coordinated technologies that
significantly reduce the cost and
complexity of developing, deploying, and
managing multitier, server-centric
applications.
• Java EE builds upon the Java SE platform
and provides a set of APIs (application
programming interfaces) for developing
and running portable, robust, scalable,
reliable and secure server-side
applications.
Đoàn Thiện Ngân Bài 6 - 6/22
What is Java EE?
Fundamental components of Java EE include:
• Enterprise JavaBeans (EJB): a managed,
server-side component architecture used to
encapsulate the business logic of an
application. EJB technology enables rapid
and simplified development of distributed,
transactional, secure and portable
applications based on Java technology.
• Java Persistence API (JPA): a framework
that allows developers to manage data using
object-relational mapping (ORM) in
applications built on the Java Platform.
Đoàn Thiện Ngân Bài 6 - 7/22
JavaScript and Ajax Development
• JavaScript is an object-oriented scripting
language primarily used in client-side
interfaces for web applications.
• Ajax (Asynchronous JavaScript and XML)
is a Web 2.0 technique that allows
changes to occur in a web page without
the need to perform a page refresh.
• JavaScript toolkits can be leveraged to
implement Ajax-enabled components and
functionality in web pages.
Đoàn Thiện Ngân Bài 6 - 8/22
JavaServer Faces 2.1 CRUD
Application from a Database
• View and modify data contained in the database -
otherwise referred to as CRUD (Create, Read,
Update, Delete) functionality. It relies on:
– JavaServer Faces (JSF) 2.0 for front-end web pages,
validation handling, and management of the
request-response cycle.
– Java Persistence API (JPA) 2.0 generates entity
classes from the database, and manage
transactions.
– Enterprise JavaBeans (EJB) 3.1, stateless EJBs that
access the entity classes, and contain the business
logic for the application.
Đoàn Thiện Ngân Bài 6 - 9/22
JavaServer Faces 2.1 CRUD
Application from a Database
• NetBeans IDE provides two wizards which
generate all of the code for the application.
– The first is the Entity Classes from
Database wizard which enables you to
generate entity classes from the provided
database.
– After creating entity classes, you use the
JSF Pages from Entity Classes wizard to
create JSF managed beans and EJBs for
the entity classes, as well as a set of
Facelets pages to handle the views for
entity class data.
Đoàn Thiện Ngân Bài 6 - 10/22
JavaServer Faces 2.1 CRUD
Application from a Database
• MySQL Database consult
•
ol/downloads/download/base/mysql-
consult.zip
• NetBeans 7
• Glassfish Server 3
• MySQL JDBC
Đoàn Thiện Ngân Bài 6 - 11/22
JavaServer Faces 2.1 CRUD
Application from a Database
• New Project:
Java Web/
Web Application
• Server:
Glassfish Server
• Java EE 6 Web
• Framework:
JavaServer Faces
• Library: JSF 2.1
Đoàn Thiện Ngân Bài 6 - 12/22
JavaServer Faces 2.1 CRUD
Application from a Database
• R-Click Project/ New/
Entity Classes from Database
Đoàn Thiện Ngân Bài 6 - 13/22
JavaServer Faces 2.1 CRUD
Application from a Database
• Data Source: jdbc/consult
• Data Tables: Add All
Đoàn Thiện Ngân Bài 6 - 14/22
JavaServer Faces 2.1 CRUD
Application from a Database
Đoàn Thiện Ngân Bài 6 - 15/22
JavaServer Faces 2.1 CRUD
Application from a Database
Đoàn Thiện Ngân Bài 6 - 16/22
JavaServer Faces 2.1 CRUD
Application from a Database
• Source Packages
• jsf.entities
– Address.java
– Billable
–
Đoàn Thiện Ngân Bài 6 - 17/22
JavaServer Faces 2.1 CRUD
Application from a Database
• R-Click Project; New/
JSF Pages from Entity Classes
Đoàn Thiện Ngân Bài 6 - 18/22
JavaServer Faces 2.1 CRUD
Application from a Database
Đoàn Thiện Ngân Bài 6 - 19/22
JavaServer Faces 2.1 CRUD
Application from a Database
Đoàn Thiện Ngân Bài 6 - 20/22
JavaServer Faces 2.1 CRUD
Application from a Database
• Tương tác với mỗi Entity
có 4 trang:
– Create.xhtml
– Edit.xhtml
– List.xhtml
– View.xhtml
• Chú ý các trang trong jsf,
jsf.sessions
Đoàn Thiện Ngân Bài 6 - 21/22
Template.xhtml
<html xmlns=""
xmlns:ui=""
xmlns:h="">
Default Title
Default Title
Default Body
Đoàn Thiện Ngân Bài 6 - 22/22
???
• JSF và NetBeans
– Entity to Class from Database Wizard
– JSF Pages From Entity Class Wizard
– Viết thử chương trình máy tính dùng JSF
– Xem cấu trúc tổ chức một project JSF trong
Netbeans
– Biên dịch (build) và chạy thử
– Tìm hiểu Administration of Glassfish Server
– Deploy từ gói ứng dụng web: *.war