Session 24. Introduction to Java Server Faces (JSF) Robert Kelly, Reading.

Similar documents
Example jsf-cdi-and-ejb can be browsed at

11.1 Introduction to Servlets

JavaServer Faces 2.0. Sangeetha S E-Commerce Research Labs, Infosys Technologies Ltd

More reading: A series about real world projects that use JavaServer Faces:

Contents. 1. JSF overview. 2. JSF example

Java EE 6 New features in practice Part 2

JSF. What is JSF (Java Server Faces)?

JSF Tags. This tutorial will cover a number of useful JSF tags. For a complete listing of available JSF tags consult the Oracle documentation at:

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

8 JSF, Images, CSS, and JS

JSF. JSF and Ajax Basics

JSF - H:SELECTONEMENU

Managed Beans III Advanced Capabilities

JSF - H:SELECTONERADIO

Refactoring to Seam. NetBeans. Brian Leonard Sun Microsystems, Inc. 14o

Using netbeans create a new Web Application and select the framework as JSF 2.2

Introduction to Java Server Faces(JSF)

Advanced Web Technology - Java Server Faces

Advanced Web Technologies 8) Facelets in JSF

Java EE 6: Develop Web Applications with JSF

Specialized - Mastering JEE 7 Web Application Development

JSF - H:PANELGRID. JSF Tag. Rendered Output. Tag Attributes. The h:panel tag renders an HTML "table" element. Attribute & Description.

CSE 336. Introduction to Programming. for Electronic Commerce. Why You Need CSE336

A Red Hat Perspective

Oracle Developer Day

Seam 3. Pete Muir JBoss, a Division of Red Hat

Université du Québec à Montréal

Very short introduction to JavaServer Faces

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

Seam & Web Beans. Pete Muir JBoss, a division of Red Hat.

Java EE 6 Community Roundtable. Reza Rahman Independent Consultant Author, EJB 3 in Action

Oracle - Developing Applications for the Java EE 7 Platform Ed 1 (Training On Demand)

Liferay Faces. Reference Documentation ga4

Implementing a Numerical Data Access Service

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

Developing Applications with Java EE 6 on WebLogic Server 12c

Session 12. RESTful Services. Lecture Objectives

Contents at a Glance

Three hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 21 st May Time:

JavaServer Faces 2.0

JSF & Struts 1, 4, 7, 2, 5, 6, 3 2, 4, 3, 1, 6, 5, 7 1, 4, 2, 5, 6, 3, 7 1, 2, 4, 5, 6, 3, 7

04/29/2004. Step by Step Guide for Building a simple JSF Application (Guess a Number) - V1.0

Java Server Pages. JSP Part II

JSF - H:INPUTSECRET. Class name of a validator that s created and attached to a component

Improve and Expand JavaServer Faces Technology with JBoss Seam

Oracle Fusion Middleware 11g: Build Applications with ADF I

Session 24. Spring Framework Introduction. Reading & Reference. dev.to/lechatthecat/how-to-use-spring-boot-java-web-framework-withintellij-idea-202p

Hello Worldwide Web: Your First JSF in JDeveloper

Visual Web Tools Reference Guide. ISBN: Publication date:

E Eclipse debugging a JSF application, 25 downloading, 2 installing, 2 launching JBoss in, 3

JavaServer Pages. What is JavaServer Pages?

A Gentle Introduction to Java Server Pages

Integrating Seam and GWT

Getting Started Guide. Version 1.7

open source community experience distilled

TheServerSide.com. Dependency Injection in Java EE 6 - Part 5

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

JSF Page Navigation. The first example we ll look at is available as jsf_ex2a.zip on your notes page.

Facelets and its use in Web Applications

Seam. Pete Muir JBoss, a Division of Red Hat.

Courses For Event Java Advanced Summer Training 2018

Building Web Applications With The Struts Framework

Oracle Fusion Middleware 11g: Build Applications with ADF I

1Z Java EE 6 Web Component Developer Certified Expert Exam Summary Syllabus Questions

Java TM. JavaServer Faces. Jaroslav Porubän 2008

Peter Norrhall. Callista Enterprise AB.

Session 9. Introduction to Servlets. Lecture Objectives

SECTION II: JAVA SERVLETS

CS506 Web Design & Development Final Term Solved MCQs with Reference

What's New in the Servlet and JavaServer Pages Technologies?

Java SE7 Fundamentals

Module 3 Web Component

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

sessionx Desarrollo de Aplicaciones en Red EL (2) EL (1) Implicit objects in EL Literals José Rafael Rojano Cáceres

STRUTS 2 - HELLO WORLD EXAMPLE

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

Liferay Faces. Reference Documentation ga2

Table of Contents Fast Track to JSF 2

JSF - Facelets Tags JSF - template tags

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Table of Contents. Introduction... xxi

Going Above and Beyond JSF 2 with RichFaces & Seam

JavaEE Interview Prep

Experiment No: Group B_2

For this week, I recommend studying Chapter 2 of "Beginning Java EE 7".

Oracle Developer Day

3.2 Example Configuration

Java EE 6 - Update Harpreet Singh GlassFish Portfolio Product Manager

FINALTERM EXAMINATION Spring 2009 CS506- Web Design and Development Solved by Tahseen Anwar

One application has servlet context(s).

Developing the First Servlet

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

JSR 299: Web Beans. Web Beans Expert Group. Version: Public Review

}w!"#$%&'()+,-./012345<ya

JavaServer Faces Programming By Budi Kurniawan READ ONLINE

UNIT -5. Java Server Page

Introduction to Seam. Pete Muir. JBoss, a division of Red Hat

Java Server Page (JSP)

Hands-on Development of Web Applications with Java EE 6

Transcription:

Session 24 Introduction to Java Server Faces (JSF) 1 Reading Reading IBM Article - www.ibm.com/developerworks/java/library/jjsf2fu1/index.html Reference Sun Tutorial (chapters 4-9) download.oracle.com/javaee/6/tutorial/doc/ Core Java Server Faces Book horstmann.com/corejsf/ API download.oracle.com/javaee/6/api/index.html 2 1

History 2002 - First JSF standard Some design problems Implemented by framework developers 2009 JSF 2.0 Includes facelets, the preferred presentation technology Corrects many design issues & simplifies programming Compatible with major frameworks More efficient 2010 - Core JSF textbook published 3 Architectural Issues MVC Architecture developed in stages Model 1 architecture jsp jphandles requests, and populates itself from a bean Model 2 architecture request is handled by a controller servlet, which populates a bean and selects a JSP for viewing data Becoming the dominant The Model 2 architecture programming style for was the basis for JSF Web applications 4 2

What is JSF? A standard to simplify Java EE development Can be implemented with drag-and-drop visual frameworks Removes much html low-level coding Allows for high-level GUI components (e.g. trees) Includes An API for representing and managing UI components Tag libraries (JSF custom tags are similar to JSP custom tags) 5 What is JSF? Manages UI components on the server side Event handling Validation Compatible with the Ajax programming model Back end data handling Designed to be used effectively by tools (NetBeans has a visual component) A major goal of JSF is to allow tool vendors to provide JSF frameworks that developers can use to build Web applications using drag and drop techniques 6 3

What is JSF? Provides a higher level abstraction for Web development Event driven programming compared to request/response Requests are converted into server side MVC for the Web events and handled by event handlers Support for client device independence UI elements are stateful objects on the server Works with multiple presentation technologies (e.g., JSP, facelets) JSP presentation is associated with JSF 1.x facelet presentation with JSF 2.0 7 JSF Separation Application code contained in beans Design contained in Web pages pg 8 4

JSFWeb Architecture Clients Web layer Facelet Bean Data sources servlet Facelet Facelet Bean DB Facelet Robert Kelly, 9 2010-2011 What Does Your Project Include? Html form Controller servlet (populate p bean, forward to JSP) Bean Code to manage the bean JavaScript/Ajax validation These are all easier to code with JSF 2.0 10 5

A managed bean Backing Beans Is a Java Bean that can be accessed from a JSF Contains properties bound to components Can include methods to handle validation, navigation, and event handling Managed when a bean name occurs in a JSF page, the implementation locates (or instantiates) the object JSF EL is used to bind a component s value to a bean property (similar to JSP EL) You have already used beans in your project 11 beanhello.xhtml Hello World JSF Facelet Facelets behaves like a JSP on steroids <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>facelet Hello World</title> </h:head> <h:body> #{hello.world} p p y </h:body> </html> Note the URL Uses the head element in the JSF library Accesses JSF h library EL expression that refers to the world property of the hello bean 12 6

JSF Hello World Bean package lectures; Looks almost like a standard Java Bean import javax.faces.bean.managedbean; @ManagedBean(name = "hello") public class HelloWorldBean { Uses Java Annotation In a way, replaces usebean final String world = "Hello World"; } public String getworld() { return world; } Alternate syntax is @Named( hello ) Bean is managed by the container and a handle to the bean is placed in the appropriate scope 13 Annotation Recap Part of Java language, starting with Java 5 Useful for metadata Annotations are tags that you insert into Java source code so that some tool can process it (not part of the normal execution of the program) Proper style places the annotation on a line preceding the statement it relates to @ManagedBean public class Hello { Think of it as a modifier for the declaration An EL reference to hello will refer to this bean You can annotate classes, methods, fields, and local variables 14 7

Annotation recap Annotations can be defined to have elements @ManagedBean(name = "hello") public class HelloWorldBean { Examples Unit testing (JUnit) JPA Mapping classes to XML Defining and using Web services Specifying deployment information 15 Annotation Sample for Bean ManagedBean (properties: name and eager) RequestScoped Rq SessionScoped 16 8

JSF Pages One JSF page per browser screen JSF page pg is either Facelets, available with JSF 2.0, are the better choice JSP originally used in JSF, but caused problems Facelet Use the xhtml extension Must be valid xhtml Use JSF tags to replace html head, body, form, and input elements 17 JSF Actions When the page is displayed, the JSF implementation locates the bean and populates the page When the page is submitted The component values are used to update the bean Control passes to the Faces servlet The Faces servlet is identified d by /faces preceding the xhtml file path name (see the example on p. 12) You can use an extension mapping to replace /faces with a.faces extension 18 9

Tag Libraries Available in Facelets ui tags for templating h JSF tags for all UI components f tags for JSF custom actions (e.g., actionlistener) c JSTL c library fn JSTL functions tags 19 h:head h:body h:form h:inputtext h:inputsecret Selected JSF Tags A majority of tags share the following attributes: id, binding, rendered, value, required, etc.) h:commandbutton (action attribute specifies url) h:outputtext h:graphicimage 20 10

EL EL was extended to support JSF Notation is slightly different, but syntax remains very similar ${mybean.property} You can also use EL to: Defer evaluation of expressions Set in addition to get Invoke methods #{mybean.property} mybean name is defined in the bean annotation 21 web.xml for JSF Hello World... <servlet> <servlet-name>faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.facesservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping>... In JSF all requests are automatically handled by a single container-generated servlet 22 11

JSF Stages Application server (e.g., Glassfish 3.0) started Client requests facelet (hello facelet is compiled) A new component tree is constructed for facelet Component tree is populated with bean contents A new view is built and rendered as a response Component tree is destroyed On postbacks, the component tree is rebuilt and saved state is applied 23 Login screen in JSF Example 24 12

Example Tasks Write a managed bean (with getters and setters) with properties of name and password Be sure to include the correct annotation in your managed bean to name the bean user and store in the session welcome.xhtml will be your facelet with the form Associate the values in your facelet with the bean properties Be sure to include the submit button the form Your welcome.xhtml facelet will refer to the name property p of the bean NetBeans will build some of the structure for you when you use the New JSF Page or new JSF Managed Bean options 25 login.xhtml Example... <h:head><title>welcome</title></h:head> <h:body> <h:form> <h3>please enter your name and password.</h3> <table> <tr> <td>name:</td> <td><h:inputtext value="#{user.name}" /></td> </tr> <tr> <td>password:</td> <td><h:inputsecret value="#{user.password}" /></td> </tr> </table> <p><h:commandbutton value="login" action="welcome"/></p> </h:form></h:body> 26 13

Example welcome.xhtml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>welcome</title> </h:head> <h:body> <h3>welcome to JSF, #{user.name}!</h3> </h:body> </html> 27 Example UserBean package lectures; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; @ManagedBean(name="user") @RequestScoped public class UserBean { private String name; private String password; public String getname() {return name;} public void setname(string newname) {name = newname;} public String getpassword() {return password;} } public void setpassword(string newpassword) { password = newpassword; } 28 14

Intro to Ajax JSF 2.0 Use additional JSF tags Include id attributes for components that are accessed by client code Provide text fields for Ajax message, which refers to a new bean property Bean property contains logic of server validation 29 Login_ajax.xhtml Example <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1- transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">> <h:head> <title>welcome</title> </h:head> 30 15

Example Does not prepend form name to ids <h:body> <h:form prependid="false"> <h3>please enter your name and password.</h3> <table> <tr> <td>name:</td> <td><h:inputtext value="#{user.name}" id="name" /> </td> </tr> <tr> <td>password:</td> Separate Ajax calls <td><h:inputsecret value="#{user.password}" id="password" /> </td></tr> </table> <p><h:commandbutton value="login" > <f:ajax execute="name password" render="out" /> </h:commandbutton></p> <h3> <h:outputtext id="out" value="#{user.greeting}"/> </h3> </h:form> </h:body> </html> Ajax execute contained within login button 31 Modified bean Example private String greeting = "Welcome to JSF + Ajax,";... public String getgreeting() { if (name.length() == 0) return ""; else return greeting + name + "!"; } 32 16