J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

30
J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Transcript of J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Page 1: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

J2EE

JavaServer Faces (JSF)

Introduction

Internetteknologi 2 (ITNET2)

Page 2: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 2

Agenda

• JavaServer Faces (JSF):– Motivation for JSF– Visual Tool Support– JSF Legacy & Elements– Events– Managed Beans– Tool support

Page 3: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 3

Motivation

• ASP.NET 2 + VS 2003/2005 – New approach to Web development– JSP/Servlet specification could not compete– JSF introduced to supplement JSP/Servlet

• JSF is a Sun Java specification– Implementations from Sun, IBM, Oracle, Exadel

• A UI framework for Java Web applications– Easy component-based UI development– Swing like event model– “Code behind” like approach– Designed for visual tool support

Page 4: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 4

Visual Tool Support from IBM WSAD, Netbeans 5.5, JDeveloper 11g

Page 5: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 5

JSF Legacy

• JSF uses JSP, EL, JSTL, Servlet technologies• It does NOT replace JSP/Servlet technology• Introduces backing beans and event programming• NOT a must• JSF works with JDBC, JPA, POJO, EJB, WS• JSF supports FrontController pattern out of the

box• STRUTS heritage• Good news – all we know may still be applied

Page 6: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 6

Elements of JSF

• FacesServlet (central processing unit)• Web.xml (well-known from JSP/Servlet J2EE)• Faces-config.xml (configures navigation and

managed beans etc.)• JSF-pages / JSF-tags (view implementations)• Managed Beans (beans controlled by framework)• Backing beans (event handler code)• Navigation Rules (front controller pattern)• Validators (same as .NET)• Converters (same as .NET)

Page 7: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 7

Faces Servlet

• Required FrontController• Implemented by the framework – not developers• All JSF request must pass through this• Configured via web.xml• Reads faces-config.xml for navigation and

managed beans

<servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup> 1 </load-on-startup></servlet>

Page navigation is NOT required to use frontController

Page 8: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 8

Faces-config.xml

• Configures navigation, managed beans etc.• May be split up into several implementations

– Managed-beans.xml– Navigation.xml– And other files

• Depending on the tool

Page 9: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 9

Managed-beans.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"><faces-config> <managed-bean> <managed-bean-name>SessionBean1</managed-bean-name> <managed-bean-class>visualebtest.SessionBean1</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>Page1</managed-bean-name> <managed-bean-class>visualebtest.Page1</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>ApplicationBean1</managed-bean-name> <managed-bean-class>visualebtest.ApplicationBean1</managed-bean-class> <managed-bean-scope>application</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>RequestBean1</managed-bean-name> <managed-bean-class>visualebtest.RequestBean1</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean></faces-config>

Backing beanOf “Page1.JSF”

Managaed Beans are accessible in allJSF pages via EL: ${Page1.submit}

Page 10: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 10

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"><faces-config> <navigation-rule> <from-view-id>/Page1.jsp</from-view-id> <navigation-case> <from-outcome>case1</from-outcome> <to-view-id>/Page2.jsp</to-view-id> </navigation-case> </navigation-rule></faces-config>

Navigation.xml

FrontController pattern implemented by defininga number of cases (actions) which e.g. a button pressMight react to – and where to navigate toCounters anti-pattern: spaghetti

Page 11: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 11

JSF Pages / Taglibs

• Open to many different technologies• In practice: JSP integration• JSF Views are JSP pages using JSF tags• XML Style• Like ASP.NET 2, UI components are represented

as different elements e.g. <h:dataTable />• Like ASP.NET – you may make your own

components

Page 12: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 12

JSF taglibs

Tablecomponent

A JSP page with JSF componentsCreated with Visual Designer

Navigation link

Binding to backing bean

Page 13: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 13

Backing Beans

• Abstracting event handling and data model away from JSF pages

• Somewhat like ”code behind” in ASP.NET

• Event Oriented• Actions• ActionListeners

Page 14: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 14

Event Handling• Actions: may change page using ”return”

– return: null for post back– return: case1 – frontcontroller -> Page2.jsf– Checks faces-config.xml or navigation.xml

• ActionListeners occur before Actions and no page change

JSF binding to action

Java Backing BeanAction Event Handler(ASP.NET = Click)

Page 15: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 15

Managed Beans

• Managed Beans are JavaBeans• JavaBeans must be reflectable

– (getter,setter, no-arg constructor)

• A Managed Bean is a Value Object pattern• Holding scoped state for the Web application• Application, Session, Request, Page1, User• Like <jsp:useBean /> in JSP• Backing Beans are Managed Beans• May be accessed from anywhere in Web application

– JSF using EL: Page1.button1– Java: Page1 page = (Page1) this.getPage1();– page.button1.setToolTip("Press for fun");

Page 16: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 16

Databinding

• A frequent task is RDBMS access• JSF has widespread support for this• May be done

– Manual: JDBC coding (see JSP slides for example)– RowSet (com.sun.sql.rowset.CachedRowSetXImpl)– Entity Beans (JPA, TopLink, Hibernate, EJB 2.1/3.0)

• Next we examplify the latter two with Netbeans 5.5 tool support

• Drag-n-drop and Wizard

Page 17: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 17

RowSet (”the qucik-n-dirty approach”)

• Most J2EE IDE tools have a Runtime tab with database acces

• Select database table and drag to designer• Creates a member in ”Session” managed bean

Page 18: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 18

Binding Table to Rowset

Page 19: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 19

JSF Resulting Tags

• A ui:Table element is added with colums• EL is used to bind the data sources

Page 20: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 20

Binding to City Drop-Down with City Table

Page 21: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 21

Alternative: Entity Classes

• RowSet approach OK for small solutions• Not good in case of business logic code• Instead: use Entity classes

– A) Wizard: Generate Entity Classes– B) Wizard: Generate JSF pages from Entity

• A) provides a domain layer (model layer)• B) will provide a control / view layer

Page 22: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 22

Entity Classes from Database

You may use this layer forOther technologies as well – Including Web services, CORBA, Java RMI, Swing, etc.

Page 23: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 23

Model Generated

• Access using JPA – write own DAO layer

Page 24: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 24

JSF Pages from Entities

• Will created View and Control / BL layers as well

Entity Manager Controls the DBAccess including transactions

Page 25: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 25

Persistence.xml

• Gets generated by tool – but can write yourself

Page 26: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 26

JSF Pages Generated

Page 27: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 27

Result

For now – JSF pages wizard does NOT work With Visual Designer applications …

Page 28: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 28

Architectural Considerations from JSF

• FrontController is directly supported – use it• Event based programming -> look out for layer

separation (do not write business logic layer code in the event handlers of the backing bean)

• Like ASP.NET – Database drag-n-drop is supported with table / component data binding

• Watch out for this approach, use only for small systems with little or no business code

• Alternative: generate Entity classes (EJB, JPA) and event JSF code

Page 29: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 29

Stefan’s Considerations on JSF

• JSF is a strong alternative to ASP.NET – but much to learn• RowSet approach (quick-n-dirty) almost as easy as Visual Studio• Entity (EE) approach much better than ASP.NET• FrontController pattern / navigation.xml is a very strong feature –

compared to master-details• JSF wins over ASP.NET for very large projects• ASP.NET / VS 2005 more productive for RAD development of small to

medium sized projects• NetBeans requires tons of RAM – but runs extremely well on 2 GB Dual

Core machine ;-) Nice to be rich …• But its FREE and runs on Solaris / Linux / Windows• Trying to develop JSF without tool support is doomed to fail!

Page 30: J2EE JavaServer Faces (JSF) Introduction Internetteknologi 2 (ITNET2)

Ingeniørhøjskolen i ÅrhusSlide 30

Advice for Assignment 1

• Consider NOT using JSF (as stated earlier)• Use JSP/Servlets – as you will learn much about

server programming from this• Try implementing a few pages also in JSF• But beware of NetBeans not working properly on

IHA Lab machines …