Bài6: Java Web Application JSF –Java EE
GV: ĐOÀN THIỆN NGÂN
ĐoànThi ệnNgân Bài6 -1/22
Nộidung
• JSF ─ Java Server Face • Entity Classes from Database Wizard • JSF Pages from Entity Classes Wizard • JSF Web application CRUD with
MySQL Database
ĐoànThi ệnNgân Bài6 -2/22
Tàili ệuthamkh ảo
1.Java™ For Programmers, 2 nd Edition,
Deitel® Developer Series, Chapters 26 & 27
2.NetBeansIDE 7 Cookbook, 2011, Packt
Publishing
3.Oracle Java EE 7 Tutorial
http://docs.oracle.com/javaee/7/tutorial/doc /jsf-intro.htm 4.NetbeansTutorial,
https://netbeans.org/kb/docs/web/jsf20- intro.html
ĐoànThi ệnNgân Bài6 -3/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ànThi ệnNgân Bài6 -4/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ànThi ệnNgân Bài6 -5/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ànThi ệnNgân Bài6 -6/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ànThi ệnNgân Bài6 -7/22
JavaServer Faces 2.1 CRUD Application from a Database http://netbeans.org/kb/docs/web/jsf20-crud.html • View and modify data contained in the database - otherwise referred to as CRUD (Create, Read, Update, Delete) functionality. It relies on: – JavaServerFaces (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ànThi ệnNgân Bài6 -8/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ànThi ệnNgân Bài6 -9/22
JavaServer Faces 2.1 CRUD Application from a Database
• MySQL Database consult • http://kenai.com/projects/consultingagencys
ol/downloads/download/base/mysql- consult.zip • NetBeans 7 • Glassfish Server 3 • MySQL JDBC
ĐoànThi ệnNgân Bài6 -10/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ànThi ệnNgân Bài6 -11/22
JavaServer Faces 2.1 CRUD Application from a Database
• R-Click Project/ New/
Entity Classes from Database …
ĐoànThi ệnNgân Bài6 -12/22
JavaServer Faces 2.1 CRUD Application from a Database
• Data Source: jdbc/consult • Data Tables: Add All
ĐoànThi ệnNgân Bài6 -13/22
JavaServer Faces 2.1 CRUD Application from a Database
ĐoànThi ệnNgân Bài6 -14/22
JavaServer Faces 2.1 CRUD Application from a Database
ĐoànThi ệnNgân Bài6 -15/22
JavaServer Faces 2.1 CRUD Application from a Database
• Source Packages • jsf.entities
– Address.java – Billable – …
ĐoànThi ệnNgân Bài6 -16/22
JavaServer Faces 2.1 CRUD Application from a Database
• R-Click Project; New/
JSF Pages from Entity Classes …
ĐoànThi ệnNgân Bài6 -17/22
JavaServer Faces 2.1 CRUD Application from a Database
ĐoànThi ệnNgân Bài6 -18/22
JavaServer Faces 2.1 CRUD Application from a Database
ĐoànThi ệnNgân Bài6 -19/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ànThi ệnNgân Bài6 -20/22
Template.xhtml
Default Title
Default Title
ĐoànThi ệnNgân Bài6 -21/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
ĐoànThi ệnNgân Bài6 -22/22