De-mystifying JavaServer™ Faces

Slides:



Advertisements
Similar presentations
Facelets Çağatay Çivici Apache MyFaces PMC
Advertisements

Apache Struts Technology
Expression Language Lec Umair Javed©2006 Generating Dynamic Contents Technologies available  Servlets  JSP  JavaBeans  Custom Tags  Expression.
ISE 390 Dynamic Web Development Java EE Web Applications.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
JBoss Seam: Contextual Components Jason Bechtel
Internet Technologies 1 Master of Information System Management Java Server Faces Model/View/Controller Design Pattern for Web Development Slides.
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
DT211/3 Internet Application Development
Java Server Faces Model/View/Controller Design Pattern for Web Development Slides adapted from “Core JavaServer Faces” by Geary and Horstmann and the J2EE.
Apache Struts Technology A MVC Framework for Java Web Applications.
JavaServer Pages TM Introduce by
Session-01. Layers Struts 2 Framework The struts 2 framework is used to develop MVC-based web application. Struts 1.0 was released in June The.
Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2.
Introduction to JavaServer Faces Jeff Swisher Director of Consulting Services Dunn Solutions Group.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
WebWork in Action An introduction to WebWork Patrick A. Lightbody.
JavaServer Faces: The Fundamentals Compiled from Sun TechDays workshops (JSF Basics, Web-Tier Codecamp: JavaServer Faces, Java Studio Creator; IBM RAD)
Java Server Faces Çağatay Çivici Apache MyFaces Team Member
Pittsburgh Java User Group– Dec Java PureFaces: A JSF Framework Extension.
Java Beans.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
1 Lecture 24 George Koutsogiannakis Spring 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
J2EE Structure & Definitions Catie Welsh CSE 432
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
Fall CIS 764 Database Systems Design L8. Web ….
Introduction to Web Dimitar Nenchev Ivan Nakov
Java Web Development with NetBeans IDE -- Kai Qian Chapter 5 JavaServer Faces (JSF) Technology.
JAVA SERVER FACES ADITI RAJORIYA UNI – ar2630. POINTS TO BE DISSCUSED WHAT IS JSF? WHY JSF? ARCHITECTURE JSF VERSIONS UI COMPONENTS JSF STRUCTURE AND.
JSF Framework Java Server Faces Presented by Songkran Totiya (6/10/2014)
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Introduction to Reasonable Server Faces Aaron Zeckoski
JavaServer Faces framework Craig McClanahan is presented that created Struts web framework and based on experience gathered designed JavaServer.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Flux & React Web Application Development Mark Repka, Rich McNeary, Steve Mueller.
SDJ INFOSOFT PVT. LTD. 2 BROWSERBROWSER JSP JavaBean DB Req Res Application Layer Enterprise server/Data Sources.
Apache Struts Technology A MVC Framework for Java Web Applications.
Project Dynamic Faces World Class AJAX for JavaServer™ Faces Technology Ed Burns Senior Staff Engineer Enterprise Java Platforms.
Intro to JavaServer Faces Mimi Opkins CECS 493 Fall 2016.
J2EE Platform Overview (Application Architecture)
J2EE Lecture 4: Advanced Concepts in JSF
Unit 6-Chapter 2 Struts.
Design and Maintenance of Web Applications in J2EE
MSIS 655 Advanced Business Applications Programming
ISE 390 Dynamic Web Development
The Model Layer What is Model?
JavaServer Faces: The Fundamentals
Presentation transcript:

De-mystifying JavaServer™ Faces Ed Burns Senior Staff Engineer Enterprise Java Platforms

Presentation Goals Impart the knowledge to enable an educated decision about JSF Teach enough to get started on a JSF project Show how to work with JSF for maximum productivity

Agenda Foundations Web Framework Classifications Four Pillars of JSF Design Patterns used in JSF Getting Stuff Done Getting Started Data Tables Spring and JSF Extending JSF UI Components non UI components

Too Many Frameworks!

Web Framework Classification Action Frameworks Struts, Struts2, Rails Hybrid Frameworks Tapestry, Wicket UI Component Frameworks JSF, Rife, Echo2

Web Framework Classification Action Frameworks

Web Framework Classification Action Frameworks You're all familiar with this kind of framework Write an action for each kind of interaction you offer to the user Usually have to write a view for each action Sometimes have a direct mapping from URL segments to actions and views http://server/controller/action/view Many implementations require subclassing framework classes for actions and forms

Web Framework Classification Hybrid Component/Action Frameworks

Web Framework Classification Hybrid Component/Action Frameworks Can be said to offer the best of both worlds Tight mapping to HTTP in actions Reuse of components Still need to code up 1:1 relationship between actions and views Some overhead for component state, but not for controllers

Web Framework Classification True UI Component Frameworks

Web Framework Classification True UI Component Frameworks Framework interacts directly with Components that comprise the view Presence of view tree allows flexible traversal and lifecycle Lots of overhead for stateful components Model dynamically referenced from components in the view.

Strengths and Weaknesses of JSF Powerful Flexible Abstraction Tool support “Black Box” components for the web I18N,L10N,A11Y Industry Standard Weaknesses Complex, overkill in some cases Different mind-set from Action based frameworks Conceptually divorced from HTTP JSP layer had problems prior to JSF 1.2 All stateful by default- performance problem

Framework Classification Discussion

The Four Pillars of JSF

The Four Pillars of JSF

The Four Pillars of JSF Views How to author them? JSP: HTML or XML syntax Facelets: XHTML syntax What are they comprised of? UI Components exposed as markup tags Nesting of tags defines UI Component Hierarchy similar to DOM, but definitely not 1:1 with DOM

The Four Pillars of JSF Views What are they comprised of? Static HTML Markup Template composition components UI Components grouped together into taglibs Can use JSTL with JSP based pages Can use JSP custom taglibs with JSP based pages How does JSF use views? one thing: build the UI Component tree Show Code of JSP and Facelet Views

The Four Pillars of JSF: View JSP Standard Syntax: not necessarily well formed XML <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %> <f:view> <html> <head><title><h:outputText value=”#{storeFront.title}” /> </title></head> <body><h:form> <h1>Items from JSTL</h1> <c:forEach var="item" items="#{input}"> <h:inputText value="#{item}" valueChangeListener="#{forEachBean1.valueChange1}"/> <br> </c:forEach> <p> <h1>Items from JSF DataTable</h1> <h:dataTable var=”item” value=”#{input}”> <h:column> <h:inputText value="#{item}" valueChangeListener="#{forEachBean1.valueChange1}"/> <br> </h:column> </h:dataTable> </h:form></body> <!-- no closing html tag --> </f:view>

The Four Pillars of JSF: View JSP XML Syntax: well formed XML <html xmlns="http://www.w3.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xml:lang="en" lang="en"> <jsp:output doctype-root-element="html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"/> <jsp:directive.page contentType="application/xhtml+xml; charset=UTF-8"/> <f:view> <html><head><title>Hello</title></head> <body bgcolor="white"><h:form> <h2>Hi. My name is Duke. I'm thinking of a number from <h:outputText lang="en_US" value="#{UserNumberBean.minimum}"/> to <h:outputText value="#{UserNumberBean.maximum}"/>. Can you guess it?</h2> <p><h:inputText id="userNo" label="User Number" value="#{UserNumberBean.userNumber}" validator="#{UserNumberBean.validate}"/> <h:commandButton id="submit" action="success" value="Submit"/></p> </body> </html> </f:view>

The Four Pillars of JSF: View Facelets: well formed XML <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xml:lang="en" lang="en"> <f:view> <html><head><ui:insert name="head"/></head> <body bgcolor="white"><h:form> <h2>Hi. My name is Duke. I'm thinking of a number from <h:outputText lang="en_US" value="#{UserNumberBean.minimum}"/> to <h:outputText value="#{UserNumberBean.maximum}"/>. Can you guess it?</h2> <p><h:inputText id="userNo" label="User Number" value="#{UserNumberBean.userNumber}" validator="#{UserNumberBean.validate}"/> <h:commandButton id="submit" action="success" value="Submit"/></p> </body> </html> </f:view>

Views - UIComponent Class Diagram The Four Pillars of JSF

The Four Pillars of JSF Views UIComponent defines semantics of a UI component Renderer defines appearance Each tag in the page is really a declaration of a pairing of a UIComponent with a Renderer The choice to use the delegated rendering approach is up to the component author

The Four Pillars of JSF

The Four Pillars of JSF Model Interaction

The Four Pillars of JSF Model Interaction - EL Use EL to “point to” properties of model objects from UI Components UI Component will get its “value” from the associated model property. Type safe conversion happens automatically Server side validation can happen if desired Model objects should only store converted and validated data You are responsible for persisting model objects

The Four Pillars of JSF Model Interaction - EL Model objects are POJOs, usually managed-beans Model objects can be “backing beans”: one per- page, but are not required to be used in this way EL Expressions operate bi-directionally “read” or “get” when rendering “write” or “set” when posting back EL allows easy integration to technologies outside of JSF: Spring, Seam, Hibernate, JNDI, etc UIData provides access to iterating over data

Spring Integration Example Model Interaction - EL Model objects are POJOs, usually managed-beans Model objects can be “backing beans”: one per- page, but are not required to be used in this way EL Expressions operate bi-directionally “read” or “get” when rendering “write” or “set” when posting back EL allows easy integration to technologies outside of JSF: Spring, Seam, Hibernate, JNDI, etc UIData provides access to iterating over data

The Four Pillars of JSF Model Interaction - EL EL implicit objects give access to web scopes and more: cookie, facesContext, header, headerValues, param, paramValues, request, requestScope, view, application, applicationScope, initParam, session, sessionScope Examples: #{requestScope.user.name}, #{prop} Widening scope lookup happens automatically Can introduce new implicit objects easily Takes full advantage of JavaBeans naming conventions for getters, setters, arrays, lists.

The Four Pillars of JSF Model Interaction - EL Can use EL in View markup Can use EL programmatically EL Can also point to component instances value style vs. component style Show code to illustrate use of EL in both styles EL can also point to methods

The Four Pillars of JSF Model Interaction – Managed Beans The built in Inversion of Control framework for JSF Can replace or supplement JSF managed beans with Spring Framework Managed beans accessed via EL are created lazily by the framework and placed into the proper “scope”: none, request, session, application Can use Java EE 5 Resource Injection into Managed Beans Show code for managed-beans

View and Model Interaction Discussion

The Four Pillars of JSF

The Four Pillars of JSF Navigation

The Four Pillars of JSF Navigation Pre-Ajax (and even post-Ajax) web apps are a modeled very well as a directed graph of web pages. The valid traversal paths for the graph can be modeled as page navigation JSF has a simple yet flexible navigation model that enables centralizing the navigation rules for the entire application in one place, yet allows easily changing these rules so the valid traversal paths are changed.

The Four Pillars of JSF Navigation Every ActionSource component can cause a page transition to occur based on the “outcome” A literal string hard coded in the page Using the EL to point to a method that takes no arguments and returns an Object that has a reasonable toString() No limit on the number of ActionSource components in a page. Each one can return a different outcome A null outcome means, “stay on the current view”

The Four Pillars of JSF Navigation The outcome is fed into a rule base and combined with the current viewId to derive where to go next, and how to get there. Example: <navigation-rule> <from-view-id>/chooseLocale.jsp</from-view-id> <navigation-case> <description> Any action on chooseLocale should cause navigation to storeFront.jsp </description> <from-outcome>storeFront</from-outcome> <to-view-id>/storeFront.jsp</to-view-id> <!-- <redirect /> --> </navigation-case> </navigation-rule>

The Four Pillars of JSF

The Four Pillars of JSF Lifecycle The Lifecycle dictates how an incoming request is handled and how a response is generated. Primarily concerned with Finding the View on which to operate Allowing components to get their values Ensuring the values are converted and validated Ensuring any event listeners are called Updating the model Selecting and rendering the new view

The Four Pillars of JSF Lifecycle You don't need to invoke the Lifecycle yourself Two methods execute Handles the postback processing for the request render Renders the view Each method uses a number of “phases” to accomplish its task.

Lifecycle “execute” portion of Lifecycle “render” portion of Lifecycle

The Four Pillars of JSF Lifecycle You can install PhaseListeners into the Lifecycle to do whatever you want before or after each or every phase. A PhaseListener has full access to the entire JSF framework, including manipulation of the view. You can use decoration to provide your own Lifecycle if you want.

Design Patterns at work in JSF Decorator Used throughout to allow customization Singleton FacesServlet, Lifecycle, ViewHandler, RenderKit, Strategy Flexible Rendering model Template Method PhaseListeners Observer java.util.EventListener

Navigation, Lifecycle and Design Patterns Discussion

Gettings Things Done Getting Started How to “get in” to JSF Config files you need Really Simple App Getting Real (thanks DHH) Listeners ActionListener ValueChangeListener Tables Spring Integration

How to “get in” to JSF Map the FacesServlet to whatever Browser makes a GET that triggers the mapping FacesServlet fires up the Lifecycle and hands control to it Lifecycle hits Render Response phase, renders view Browser makes one or more POST requests which, by virture of having been rendered by JSF, will automatically hit the FacesServlet.

Config Files you need WEB-INF/web.xml Required for declaring FacesServlet Optionally map FacesServlet Optionally pass init params to JSF runtime State saving method Alternate Lifecycle XML validation of faces-config files Implementation specific options Anything else for which you normally use web.xml Resource declarations Security options

Config Files you need WEB-INF/faces-config.xml Don't need it for really simple apps without: managed-beans, non-jarred {components, converters, validators, renderers, etc}, navigation Declares any non-jarred JSF artifacts, Example: j1 app navigation-rules Let's look at the DTD (schemas are not human readable!) For regular apps that don't need to extend, all you really need to know is managed beans We'll cover the rest later.

Simple Toy App helloDuke - files web/WEB-INF/web.xml web/WEB-INF/faces-config.xml web/index.html web/greeting.jsp web/response.jsp web/wave.med.gif src/helloDuke/UserNameBean.java

Getting Real Listeners - ActionListener General use: MethodExpression in view <bp:scroller for="datagrid" actionListener="#{orderEnt.scrollDataGrid}"> Java code on managed bean public void scrollDataGrid(ActionEvent e) { if (this.data != null) { int rows = this.data.getRows(); if (rows < 1) return; if (currentRow < 0) this.data.setFirst(0); else if ...} }

Getting Real Listeners - ActionListener Also have f:actionListener tag Names an actual class that must implement ActionListener Example: J1 order entry scroller

Getting Real Listeners - ValueChangeListener General use: MethodExpression in view <a:ajax name="dojo.fisheye" value="#{bean.selectedIndex}" valueChangeListener="#{bean.valueChanged}"/> Java code on managed bean public void valueChanged(ValueChangeEvent e) { ... do something with the event }

Getting Real Listeners - ActionListener Also have f:valueChangeListener tag Names an actual class that must implement ValueChangeListener Example: Dojo Fisheye widget

Getting Real Displaying Tabular Data Use <h:dataTable value=”#{foo.dataModel}” /> #{foo.dataModel} points to an instance of javax.faces.model.DataModel Which has subclasses: ArrayDataModel, ListDataModel, ResultDataModel (JSTL Result ), ResultSetDataModel (java.sql.ResultSet), ScalarDataModel (wraps Java Object) #{foo.dataModel} points to an array, List, Result (JSTL), ResultSet, Object. In this case, DataModel instance is constructed automatically

Getting Real Displaying Tabular Data Example: Order Line system from J1 demo

Getting Real Spring Integration Options Replace JSF's managed-bean facility with Spring via the SpringVariableResolver. Use JSF and Spring MVC together in the same application Replace JSF's navigation facility with Spring Web Flow via a custom Navigation Handler (prototype concept!)

Listeners, Tables, Spring Integration

Extending JSF UI Components Without delegated rendering With delegated rendering Non-UI Components ActionListener,ELResolver, {Variable,Property}Resolver, FacesContextFactory, RenderKit, NavigationHandler, ViewHandler Packaging components

Custom UI Components Without Delegated Rendering Extend UIComponent or UIComponentBase Important methods encode*(), decode(), saveState(), restoreState() Declare component in faces-config.xml <component> <component-type>Chart</component-type> <component-class>com.foo.ChartComponent </component-class> </component>

Custom UI Components With Delegated Rendering Extend UIComponent or UIComponentBase Do not implement encode*() or decode*() Important methods saveState(), restoreState() Component declaration same as before Extend Renderer

Custom UI Components With Delegated Rendering Extend Renderer Show MapRenderer Declare in faces-config.xml <render-kit> <renderer> <component-family>ProgressBar</component-family> <renderer-type>ProgressBar</renderer-type> <renderer-class>com.foo.ChartRenderer </renderer-class> </renderer> </render-kit>

Custom Non-UI Components ActionListener,ELResolver, {Variable,Property}Resolver, FacesContextFactory, RenderKit, NavigationHandler, ViewHandler Back to the DTD

Packaging components in a JAR Key concept: JSF Runtime must look for META-INF/faces-config.xml file in every JAR file in WEB-INF/lib at web app deployment time Wide range of COTS components (both UI and non- UI) see JSFCentral.com

Summary JSF is a component based framework JSF is easy to understand if you know the fundamentals View Model Interaction Navigation Lifecycle https://javaserverfaces.dev.java.net/ The Best of Both Worlds

De-Mystifying JSF Ed Burns ed.burns@sun.com