SlideShare a Scribd company logo
JSF (JavaServer Faces) Martin Kurtev National Academy for Software Development academy.devbg.org Vladimir Tsanev Part 1 – Basics
Contents JSP Architecture Models Model 1 and Model 2 (MVC) Introduction to JSF What is JSF? Faces Servlet Request Lifecycle – Quick Overview JSF Hello World Application Application Configuration File
JSP Architectures Model 1
Model 1 Web browser directly accessing Web-tier JSP pages The JSP pages access Web-tier JavaBeans that represent the application model
Model 1 (2) The next view to display is determined by Hyperlinks selected in the source document  Request parameters Application control is decentralized  Current page being displayed determines the next page to display Each JSP page or servlet processes its own inputs  Parameters from GET or POST
Model 1 (When to Use?) Provide a more lightweight design for small, static applications Applications which  Have very simple page flow Have little need for centralized security control or logging Changes little over time
JSP Architectures Model 2
Model 2 (MVC) Introduces a controller servlet between the browser and the JSP pages or servlet content being delivered Views do not refer to each other directly
Controller Servlet Centralizes logic for Dispatching  of requests to the next view based on the request URL Input parameters Application  state Handles view selection Decouples JSP pages and servlets from one another Provides Single point of control for security and logging Encapsulation of incoming data into a form usable by the back-end MVC model
Model 2 - Advantages Easier to maintain and extend There are many ready frameworks so we do not have to write our own Struts Tapestry Spring Web Flow WebWork JavaServer Faces …  and many others
Introduction to JavaServer Faces What is JSF?
What is JavaServer Faces? JavaServer Faces is: Web Application Framework Request-driven MVC Uses component-based approach Uses JSP for its display technology, but is not limited to it
What is JavaServer Faces? (2) JSF Includes Set of APIs Two JSP custom tag libraries for expressing UI within JSP Server-side event model State management Managed beans Unified Expression Language
Faces Servlet The  FacesServlet   A ccepts  all  incoming  JSF  requests I nitializes resources P asses  requests  to the  request  lifecycle for processing Faces servlet plays the role of the controller servlet in MVC architecture
Faces Servlet - Mapping Mapping for the  FacesServlet  in the  web.xml There are two standard ways to map the faces servlet / faces/*  - prefix mapping *.jsf  – suffix mapping <servlet> <display-name>FacesServlet</display-name>  <servlet-name>FacesServlet</servlet-name>  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>FacesServlet</servlet-name>  <url-pattern> / some-url-pattern </url-pattern>  </servlet-mapping>
JSF View A JSF page is represented by a tree of UI components, called a  view Almost always the view is described with JSP pages (but not mandatory) When a client makes a request for the page, if it is caught by the Faces Servlet and the JSF request lifecycle starts During the lifecycle, JSF implementation must build the view considering the state saved from the previous postback
Lifecycle Phases Request lifecycle consists of 6 phases: Restore View Phase Apply Request Values Phase Process Validations Phase Update Model Values Phase Invoke Application Phase Render Response Phase These phases are described in details at the end of the lecture
Request Lifecycle Diagram
JSF Hello World Application
JSF Hello Application To make the simplest JSF application you should perform the following steps: Create new Web project If your application container does not support JSF (for example Tomcat) you should add the JSF API and Implementation in your  lib  folder JSF 1.2 also requires JSTL 1.2 in your libraries For my-faces following jars are also required: commons-beanutils-1.7.0.jar ,  commons-codec-1.3.jar ,  commons-collections-3.2.jar ,  commons-digester-1.8.jar ,  commons-discovery-0.4.jar ,  commons-logging-1.1.1.jar
WEB-INF/lib contents This is the structure of our hello JSF application MyFaces JSF API and Implementation jars JARs required by MyFaces JSTL 1.2
web.xml In the  web.xml  file we define Welcome file –  index.html Define Faces Servlet and its mapping <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup> 0 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern> *.jsf </url-pattern> </servlet-mapping> If non-negative, the Web container will load this servlet on deployment Faces Servlet is mapped to all requests that ends with  .jsf
index.html and hello.jsp index.html When Faces Servlet sees  *.jsf  it will look for  *. jsp  to create the view In  hello.jsp  all content that is JSF specific should be in enclosed in  < f:view >  tag We also need to specify the needed JSP tag libraries <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> <head><title>Welcome to JSF Hello World Demo</title></head> <body> <a href=&quot; hello.jsf &quot;>Go to Hello JSF Page</a> </body> </html>
hello.jsp Here we do 3 things: Define &quot;JSF core&quot; and &quot;JSF html&quot; tag libraries JSP view ( f:view  tag) Use  outputText  component to show message <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?> <jsp:root   xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot; xmlns:f=&quot;http://java.sun.com/jsf/core&quot; xmlns:h=&quot;http://java.sun.com/jsf/html&quot;  version=&quot;2.1&quot;> ... <f:view> < h:outputText value=&quot;#{'Hello World! I am a h:outputText message'}&quot; /> </f:view> ... </jsp:root>
Live Demo JSF Hello World Application
Application Configuration File  ( faces-config.xml ) faces-config.xml  d efines  P age navigation rules  C onfigures  managed  beans  O ther custom objects, such as custom components It is located in the  WEB-INF  folder of the project Basic structure: <?xml version=&quot;1.0&quot;?> <faces-config xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;   xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd&quot;   version=&quot;1.2&quot;>   ... </faces-config>
JSF Component Model
Component Model A set of  UIComponent classes  for specifying the state and behaviour of UI components A  rendering model  that defines how to render the components in different ways One UI component may have several presentations Example:  UICommand  – button and hyperlink
Component Model (2) A server-side  event-listener model  that defines how to handle UI component events A  conversion model  that defines how to plug in data converters onto a component A  validation model  that defines how to register validators onto a component
JSF UI Components
What is an UI Component? Configurable, reusable elements that compose the user interfaces of JSF applications UI components can be: Simple, like a button or text box Compound, like a table, which can be composed of multiple other components Accessible via JSF tags in JSP page
UI Component Classes The JSF implementation provides a set of UI component classes Developers can extend the UI component classes to create custom UI components Third party component libraries are available: RichFaces, ICEfaces, etc. All JSF UI component classes extend  UIComponentBase UIComponentBase  defines the default state and behaviour of an UIComponent
UI Component Classes UI component classes specify all of the UI component functionality Retrieving values from input form (decoding) ‏ Holding component state Maintaining a reference to model objects  Driving event-handling Rendering – creating markup (encoding) ‏
UI Component Classes (2) A JSF UI component is typically a collection of classes and resources: UIComponent Class  –  the core logic of the component , e.g.  HtmlCommandLink Renderer Class , e.g.  HtmlLink Renderer UI Component Tag Class , e. g.  HtmlCommandLinkTag Tag Library Descriptor File  ( *.tld ), e.g.  html-basic.tld Other classes: converters, validators, action listeners, etc.
Component Rendering Model Usually the components do not specify how they are rendered Each component can have multiple renderers which render it to one or more clients Render Kit – defines how components are rendered for a particular client The JSF implementation includes a standard HTML Render Kit for rendering its components to an HTML client
JSF Commonly Used Tags
Commonly Used Tags – JSF Core Library <f:view>  – root element for all JSF pages <f:converter>  – creates an instance of the specified converter and binds it to its parent component <f:validator>  – creates an instance of the specified validator and binds it to its parent component ( UIInput  component) <f:actionListener>  – creates an instance of the action listener and binds it to its parent component
Commonly Used Tags – JSF Core Library (2) <f:param>  – creates name-value pair for its parent component <f:loadBundle>  – loads a specified bundle and stores it in the request scope <f:subview>  –  acts as a naming container so that the components inside it can be made unique Use it to import another JSF page
Commonly Used Tags – JSF HTML Library <h:form>  – used as a container for elements which send data (HTML form) <h:inputText> ,  <h:inputSecret>, <h:inputHidden>  – used to input text, passwords and hidden values <h:outputText> ,  <h:outputLabel>  – used to output text <h:commandButton> ,  <h:commandLink>  – renders a button or hyperlink <h:graphicImage>  – renders an image
Commonly Used Tags – JSF HTML Library (2) <h:dataTable>   –  creates HTML table <h:column>   –  used as child element of  <h:dataTable> <h:selectBooleanCheckBox>   –  renders as checkbox <h:message>   –  renders the first  FacesMessage  assigned to the component defined in the  for  attribute <h:selectManyCheckBox>  - renders as list of checkboxes Elements stored in  <f:selectItem> / <f:selectItems>
Live Demo HTML UI Components
Managed Beans
Managed Beans Managed beans are JavaBeans which: Provide the logic for initializing and controlling JSF components  Data binding, action listeners, validation, conversion, navigation, etc. Manage data across page requests, user sessions, or the application as a whole C reated by JSF  and s tored within the  request ,  session  or  application Also called &quot;backing beans&quot;
Mapping Managed Beans Managed beans are mapped in the  faces-config.xml Property value can refer to some other managed bean (or its  property) using EL  <value>#{ otherBean }</value> <managed-bean> <managed-bean-name>someName</managed-bean-name> <managed-bean-class>package.BeanClass</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>minimum</property-name> <property-class>long</property-class> <value>0</value> </managed-property> </managed-bean>
Mapping Elements <managed-bean>   –  enclosing element <managed-bean-name>  – this element’s value is the identifier used for the bean in our JSP pages <managed-bean-class>  – the fully qualified name of the class of the bean <managed-bean-scope>  – the bean’s scope (request, session, application, none)
Mapping Elements (2) <managed-property>  –  property enclosing element <property-name>  – this element’s value is the name of a bean property <property-class>  – the fully qualified name of the class of the property <value>  – value of the property If the value is another managed bean then the scope of the other bean should be greater than the scope of the bean
Binding Values Managed beans and their properties can be used as values for the components Example: we have a session scoped managed bean of class  UserBean  with property  userName  we can do JSF will automatically apply component entered value to the  userName  property and vice versa <h:inputText id=&quot;userNameInput&quot; value=&quot; #{userBean.userName} &quot; />
Managed Beans – Example public class ApplicationInfoBean implements Serializable { private static final long   serialVersionUID =  1 L; private String info; // Getters and setters come here } public class UserBean implements Serializable { private String userName; private ApplicationInfoBeanapplicationInfoBean; // Getters and setters come here } We have two JavaBeans:
Managed Beans – Example <managed-bean> <managed-bean-name>userBean</managed-bean-name> <managed-bean-class>jsfdemo.UserBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>applicationInfoBean</property-name> <property-class>jsfdemo.ApplicationInfoBean</property-class> <value>#{applicationInfoBean}</value> </managed-property> </managed-bean> <managed-bean> <managed-bean-name>applicationInfoBean</managed-bean-name> <managed-bean-class>jsfdemo.ApplicationInfoBean</managed-bean-class> <managed-bean-scope>application</managed-bean-scope> </managed-bean> Register the beans in  faces-config.xml :
Managed Beans – Example <h:outputFormat value=&quot;Hello, {0}&quot;> <f:param value=&quot;#{userBean.userName}&quot; /> </h:outputFormat> <h:outputFormat value=&quot;Application Info: {0}&quot;> <f:param value=&quot;#{applicationInfoBean.info}&quot; /> </h:outputFormat> <h:form id=&quot;userNameForm&quot;> <h:outputLabel for=&quot;userInput&quot; value=&quot;User Name:&quot; /> <h:inputText id=&quot;userInput&quot; value=&quot;#{userBean.userName}&quot; /> <h:commandButton value=&quot;Apply&quot; /> </h:form> <h:form id=&quot;appInfoForm&quot;> <h:outputLabel for=&quot;appInfoInput&quot; value=&quot;Application Info:&quot; /> <h:inputText id=&quot;appInfoInput&quot; value=&quot;#{userBean.applicationInfoBean.info}&quot; /> <h:commandButton value=&quot;Apply&quot; /> </h:form> Bind JSF controls with the beans:
Live Demo Managed Beans
Simple JSF Sumator Using JSF binding and the Unified Expression Language and to sum numbers: <f:view> <h:form> <h:inputText value=&quot;#{firstNum}&quot; /> + <h:inputText value=&quot;#{secondNum}&quot; /> <h:commandButton value=&quot;OK&quot; /> </h:form> <h:outputFormat value=&quot; S um  =  {0}&quot; > <f:param value=&quot;#{firstNum + secondNum}&quot; /> </h:outputFormat> </f:view>
Live Demo JSF Sumator
JSF Navigation Model
What Is Navigation? Navigation is a set of rules for choosing the next page to be displayed Applied after a button or hyperlink is clicked The selection of the next page is determined by: The page that is currently displayed The action method invoked by the action property of the component that generated the event An outcome string that was returned by the action method or passed from the component
Navigation Elements in  faces-config.xml <from-view-id>   element defines the source page.  May be a pattern. For example  /* . This will cause all JSF pages to redirect to some view on given outcome. <from-outcome>   element defines the logical outcome as specified in the  action  attribute of the event source <to-view-id>   element defines the page to be displayed when the specified outcome is returned <from-action>  element refers to an action method that returns a  String , which is the logical outcome
Navigation Rules – Example <navigation-rule> <from-view-id> /* </from-view-id> <navigation-case> <from-outcome> home </from-outcome> <to-view-id> /navigation-demo.jsp </to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id> /navigation-demo.jsp </from-view-id> <navigation-case> <from-outcome> login_success </from-outcome> <to-view-id> /logged.jsp </to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id> /navigation-demo.jsp </from-view-id> <navigation-case> <from-outcome> login_failed </from-outcome> <to-view-id > /login-failed.jsp </to-view-id> </navigation-case> </navigation-rule>
Action Attribute in JSF Form To specify what to be the form outcome you can Provide a constant string as action attribute of an event source Provide a managed bean method with no parameters which returns  String Using this approach you can add some logic in this method that returns different result in different situations <h:commandButton value=&quot;Next Page&quot; action=&quot; nextPage &quot; /> <h:commandButton value=&quot;Login&quot; action=&quot; #{userBean.login} &quot; />
Live Demo Navigation Model
Creating JSF Wizard Step 1 Enter first name, last name Step 2 Enter phone, email Final step Display all entered data We will use managed bean  Person  with session scope to store entered data We will use navigation rules for the active page
Live Demo JSF Wizard
JavaServer Faces Questions?
Problems Create a simple JSF project which accepts your name and outputs it on the same page. Write a JSF based Web application which has several forms for entering information about a person. The first page must contain a form for entering name, age, country, town, occupation. The second page must contain a form for entering street address, town and country. The last page must show all collected information. Use JSF managed beans  PersonInfo  and  Address  for storing in the session all data entered in the forms.

More Related Content

PPTX
Jsf presentation
PPSX
Spring - Part 1 - IoC, Di and Beans
PPTX
Introduction to spring boot
DOCX
Spring notes
PPTX
Hibernate ppt
 
PPTX
Express js
PDF
Introduction to Spring Boot!
PPTX
Spring boot
Jsf presentation
Spring - Part 1 - IoC, Di and Beans
Introduction to spring boot
Spring notes
Hibernate ppt
 
Express js
Introduction to Spring Boot!
Spring boot

What's hot (20)

PDF
Spring Framework - MVC
PPTX
Spring boot
PPS
Java Hibernate Programming with Architecture Diagram and Example
PPTX
Introduction to Spring Boot
PDF
Spring boot introduction
PPT
Jsp ppt
PDF
Spring Framework - AOP
PPTX
ReactJS presentation.pptx
PPT
Hibernate
 
PPTX
Nodejs functions & modules
PDF
Spring Data JPA
PPTX
Angular Data Binding
PPTX
REST API
PPTX
REST & RESTful Web Services
PPT
SOLID Design Principles
PPTX
Spring boot Introduction
PDF
Java 8 features
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
PDF
Spring Framework - Core
Spring Framework - MVC
Spring boot
Java Hibernate Programming with Architecture Diagram and Example
Introduction to Spring Boot
Spring boot introduction
Jsp ppt
Spring Framework - AOP
ReactJS presentation.pptx
Hibernate
 
Nodejs functions & modules
Spring Data JPA
Angular Data Binding
REST API
REST & RESTful Web Services
SOLID Design Principles
Spring boot Introduction
Java 8 features
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Spring Framework - Core
Ad

Viewers also liked (15)

PDF
Ruby on Rails for beginners
PPTX
Java script
PPTX
Data warehouse,data mining & Big Data
PPTX
Java server faces
 
PDF
Ruby on Rails Presentation
PPTX
PDF
Ruby on Rails Presentation
PPT
Web servers
PPTX
9. java server faces
PPT
Javascript
PPT
Ajax Ppt
PDF
JavaScript Functions
PPTX
Java Script (shqip)
PPTX
Ajax ppt - 32 slides
PPT
Web Servers (ppt)
Ruby on Rails for beginners
Java script
Data warehouse,data mining & Big Data
Java server faces
 
Ruby on Rails Presentation
Ruby on Rails Presentation
Web servers
9. java server faces
Javascript
Ajax Ppt
JavaScript Functions
Java Script (shqip)
Ajax ppt - 32 slides
Web Servers (ppt)
Ad

Similar to Java Server Faces (JSF) - Basics (20)

PPTX
PDF
Introduction to jsf2
PPTX
Introduction to jsf 2
PPT
MVC
 
PPTX
ODP
Sprint Portlet MVC Seminar
PPTX
Spring Framework
 
PDF
AK 4 JSF
PDF
AK 5 JSF 21 july 2008
ODP
Spring Portlet MVC
PPTX
Introduction to JSF
PPTX
Introduction to ejb and struts framework
PPT
JSF and Seam
 
PPT
J2EE - JSP-Servlet- Container - Components
PDF
Java server faces
PDF
Spring mvc
PPTX
Struts & spring framework issues
PDF
jsf2 Notes
PPT
Struts 2 Overview
PPT
Introduction to the Servlet / JSP course
Introduction to jsf2
Introduction to jsf 2
MVC
 
Sprint Portlet MVC Seminar
Spring Framework
 
AK 4 JSF
AK 5 JSF 21 july 2008
Spring Portlet MVC
Introduction to JSF
Introduction to ejb and struts framework
JSF and Seam
 
J2EE - JSP-Servlet- Container - Components
Java server faces
Spring mvc
Struts & spring framework issues
jsf2 Notes
Struts 2 Overview
Introduction to the Servlet / JSP course

More from BG Java EE Course (20)

PPT
Rich faces
PPT
JSP Custom Tags
PPT
Java Server Faces (JSF) - advanced
PPT
Unified Expression Language
PPT
Java Server Pages
PPT
Web Applications and Deployment
PPT
Java Servlets
PPTX
HTML: Tables and Forms
PPTX
HTML Fundamentals
PPTX
WWW and HTTP
ODP
JavaScript and jQuery Fundamentals
ODP
Creating Web Sites with HTML and CSS
PPT
Processing XML with Java
PPT
Introduction to XML
PPT
Data Access with JDBC
PPT
Introduction to-sql
PPT
Introduction to-RDBMS-systems
PPT
Basic data-structures-v.1.1
Rich faces
JSP Custom Tags
Java Server Faces (JSF) - advanced
Unified Expression Language
Java Server Pages
Web Applications and Deployment
Java Servlets
HTML: Tables and Forms
HTML Fundamentals
WWW and HTTP
JavaScript and jQuery Fundamentals
Creating Web Sites with HTML and CSS
Processing XML with Java
Introduction to XML
Data Access with JDBC
Introduction to-sql
Introduction to-RDBMS-systems
Basic data-structures-v.1.1

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
KodekX | Application Modernization Development
 
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced IT Governance
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KodekX | Application Modernization Development
 
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Sensors and Actuators in IoT Systems using pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx
madgavkar20181017ppt McKinsey Presentation.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced IT Governance
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...

Java Server Faces (JSF) - Basics

  • 1. JSF (JavaServer Faces) Martin Kurtev National Academy for Software Development academy.devbg.org Vladimir Tsanev Part 1 – Basics
  • 2. Contents JSP Architecture Models Model 1 and Model 2 (MVC) Introduction to JSF What is JSF? Faces Servlet Request Lifecycle – Quick Overview JSF Hello World Application Application Configuration File
  • 4. Model 1 Web browser directly accessing Web-tier JSP pages The JSP pages access Web-tier JavaBeans that represent the application model
  • 5. Model 1 (2) The next view to display is determined by Hyperlinks selected in the source document Request parameters Application control is decentralized Current page being displayed determines the next page to display Each JSP page or servlet processes its own inputs Parameters from GET or POST
  • 6. Model 1 (When to Use?) Provide a more lightweight design for small, static applications Applications which Have very simple page flow Have little need for centralized security control or logging Changes little over time
  • 8. Model 2 (MVC) Introduces a controller servlet between the browser and the JSP pages or servlet content being delivered Views do not refer to each other directly
  • 9. Controller Servlet Centralizes logic for Dispatching of requests to the next view based on the request URL Input parameters Application state Handles view selection Decouples JSP pages and servlets from one another Provides Single point of control for security and logging Encapsulation of incoming data into a form usable by the back-end MVC model
  • 10. Model 2 - Advantages Easier to maintain and extend There are many ready frameworks so we do not have to write our own Struts Tapestry Spring Web Flow WebWork JavaServer Faces … and many others
  • 11. Introduction to JavaServer Faces What is JSF?
  • 12. What is JavaServer Faces? JavaServer Faces is: Web Application Framework Request-driven MVC Uses component-based approach Uses JSP for its display technology, but is not limited to it
  • 13. What is JavaServer Faces? (2) JSF Includes Set of APIs Two JSP custom tag libraries for expressing UI within JSP Server-side event model State management Managed beans Unified Expression Language
  • 14. Faces Servlet The FacesServlet A ccepts all incoming JSF requests I nitializes resources P asses requests to the request lifecycle for processing Faces servlet plays the role of the controller servlet in MVC architecture
  • 15. Faces Servlet - Mapping Mapping for the FacesServlet in the web.xml There are two standard ways to map the faces servlet / faces/* - prefix mapping *.jsf – suffix mapping <servlet> <display-name>FacesServlet</display-name> <servlet-name>FacesServlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>FacesServlet</servlet-name> <url-pattern> / some-url-pattern </url-pattern> </servlet-mapping>
  • 16. JSF View A JSF page is represented by a tree of UI components, called a view Almost always the view is described with JSP pages (but not mandatory) When a client makes a request for the page, if it is caught by the Faces Servlet and the JSF request lifecycle starts During the lifecycle, JSF implementation must build the view considering the state saved from the previous postback
  • 17. Lifecycle Phases Request lifecycle consists of 6 phases: Restore View Phase Apply Request Values Phase Process Validations Phase Update Model Values Phase Invoke Application Phase Render Response Phase These phases are described in details at the end of the lecture
  • 19. JSF Hello World Application
  • 20. JSF Hello Application To make the simplest JSF application you should perform the following steps: Create new Web project If your application container does not support JSF (for example Tomcat) you should add the JSF API and Implementation in your lib folder JSF 1.2 also requires JSTL 1.2 in your libraries For my-faces following jars are also required: commons-beanutils-1.7.0.jar , commons-codec-1.3.jar , commons-collections-3.2.jar , commons-digester-1.8.jar , commons-discovery-0.4.jar , commons-logging-1.1.1.jar
  • 21. WEB-INF/lib contents This is the structure of our hello JSF application MyFaces JSF API and Implementation jars JARs required by MyFaces JSTL 1.2
  • 22. web.xml In the web.xml file we define Welcome file – index.html Define Faces Servlet and its mapping <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup> 0 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern> *.jsf </url-pattern> </servlet-mapping> If non-negative, the Web container will load this servlet on deployment Faces Servlet is mapped to all requests that ends with .jsf
  • 23. index.html and hello.jsp index.html When Faces Servlet sees *.jsf it will look for *. jsp to create the view In hello.jsp all content that is JSF specific should be in enclosed in < f:view > tag We also need to specify the needed JSP tag libraries <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> <head><title>Welcome to JSF Hello World Demo</title></head> <body> <a href=&quot; hello.jsf &quot;>Go to Hello JSF Page</a> </body> </html>
  • 24. hello.jsp Here we do 3 things: Define &quot;JSF core&quot; and &quot;JSF html&quot; tag libraries JSP view ( f:view tag) Use outputText component to show message <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?> <jsp:root xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot; xmlns:f=&quot;http://java.sun.com/jsf/core&quot; xmlns:h=&quot;http://java.sun.com/jsf/html&quot; version=&quot;2.1&quot;> ... <f:view> < h:outputText value=&quot;#{'Hello World! I am a h:outputText message'}&quot; /> </f:view> ... </jsp:root>
  • 25. Live Demo JSF Hello World Application
  • 26. Application Configuration File ( faces-config.xml ) faces-config.xml d efines P age navigation rules C onfigures managed beans O ther custom objects, such as custom components It is located in the WEB-INF folder of the project Basic structure: <?xml version=&quot;1.0&quot;?> <faces-config xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;   xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd&quot;   version=&quot;1.2&quot;>   ... </faces-config>
  • 28. Component Model A set of UIComponent classes for specifying the state and behaviour of UI components A rendering model that defines how to render the components in different ways One UI component may have several presentations Example: UICommand – button and hyperlink
  • 29. Component Model (2) A server-side event-listener model that defines how to handle UI component events A conversion model that defines how to plug in data converters onto a component A validation model that defines how to register validators onto a component
  • 31. What is an UI Component? Configurable, reusable elements that compose the user interfaces of JSF applications UI components can be: Simple, like a button or text box Compound, like a table, which can be composed of multiple other components Accessible via JSF tags in JSP page
  • 32. UI Component Classes The JSF implementation provides a set of UI component classes Developers can extend the UI component classes to create custom UI components Third party component libraries are available: RichFaces, ICEfaces, etc. All JSF UI component classes extend UIComponentBase UIComponentBase defines the default state and behaviour of an UIComponent
  • 33. UI Component Classes UI component classes specify all of the UI component functionality Retrieving values from input form (decoding) ‏ Holding component state Maintaining a reference to model objects Driving event-handling Rendering – creating markup (encoding) ‏
  • 34. UI Component Classes (2) A JSF UI component is typically a collection of classes and resources: UIComponent Class – the core logic of the component , e.g. HtmlCommandLink Renderer Class , e.g. HtmlLink Renderer UI Component Tag Class , e. g. HtmlCommandLinkTag Tag Library Descriptor File ( *.tld ), e.g. html-basic.tld Other classes: converters, validators, action listeners, etc.
  • 35. Component Rendering Model Usually the components do not specify how they are rendered Each component can have multiple renderers which render it to one or more clients Render Kit – defines how components are rendered for a particular client The JSF implementation includes a standard HTML Render Kit for rendering its components to an HTML client
  • 37. Commonly Used Tags – JSF Core Library <f:view> – root element for all JSF pages <f:converter> – creates an instance of the specified converter and binds it to its parent component <f:validator> – creates an instance of the specified validator and binds it to its parent component ( UIInput component) <f:actionListener> – creates an instance of the action listener and binds it to its parent component
  • 38. Commonly Used Tags – JSF Core Library (2) <f:param> – creates name-value pair for its parent component <f:loadBundle> – loads a specified bundle and stores it in the request scope <f:subview> – acts as a naming container so that the components inside it can be made unique Use it to import another JSF page
  • 39. Commonly Used Tags – JSF HTML Library <h:form> – used as a container for elements which send data (HTML form) <h:inputText> , <h:inputSecret>, <h:inputHidden> – used to input text, passwords and hidden values <h:outputText> , <h:outputLabel> – used to output text <h:commandButton> , <h:commandLink> – renders a button or hyperlink <h:graphicImage> – renders an image
  • 40. Commonly Used Tags – JSF HTML Library (2) <h:dataTable> – creates HTML table <h:column> – used as child element of <h:dataTable> <h:selectBooleanCheckBox> – renders as checkbox <h:message> – renders the first FacesMessage assigned to the component defined in the for attribute <h:selectManyCheckBox> - renders as list of checkboxes Elements stored in <f:selectItem> / <f:selectItems>
  • 41. Live Demo HTML UI Components
  • 43. Managed Beans Managed beans are JavaBeans which: Provide the logic for initializing and controlling JSF components Data binding, action listeners, validation, conversion, navigation, etc. Manage data across page requests, user sessions, or the application as a whole C reated by JSF and s tored within the request , session or application Also called &quot;backing beans&quot;
  • 44. Mapping Managed Beans Managed beans are mapped in the faces-config.xml Property value can refer to some other managed bean (or its property) using EL <value>#{ otherBean }</value> <managed-bean> <managed-bean-name>someName</managed-bean-name> <managed-bean-class>package.BeanClass</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>minimum</property-name> <property-class>long</property-class> <value>0</value> </managed-property> </managed-bean>
  • 45. Mapping Elements <managed-bean> – enclosing element <managed-bean-name> – this element’s value is the identifier used for the bean in our JSP pages <managed-bean-class> – the fully qualified name of the class of the bean <managed-bean-scope> – the bean’s scope (request, session, application, none)
  • 46. Mapping Elements (2) <managed-property> – property enclosing element <property-name> – this element’s value is the name of a bean property <property-class> – the fully qualified name of the class of the property <value> – value of the property If the value is another managed bean then the scope of the other bean should be greater than the scope of the bean
  • 47. Binding Values Managed beans and their properties can be used as values for the components Example: we have a session scoped managed bean of class UserBean with property userName we can do JSF will automatically apply component entered value to the userName property and vice versa <h:inputText id=&quot;userNameInput&quot; value=&quot; #{userBean.userName} &quot; />
  • 48. Managed Beans – Example public class ApplicationInfoBean implements Serializable { private static final long serialVersionUID = 1 L; private String info; // Getters and setters come here } public class UserBean implements Serializable { private String userName; private ApplicationInfoBeanapplicationInfoBean; // Getters and setters come here } We have two JavaBeans:
  • 49. Managed Beans – Example <managed-bean> <managed-bean-name>userBean</managed-bean-name> <managed-bean-class>jsfdemo.UserBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>applicationInfoBean</property-name> <property-class>jsfdemo.ApplicationInfoBean</property-class> <value>#{applicationInfoBean}</value> </managed-property> </managed-bean> <managed-bean> <managed-bean-name>applicationInfoBean</managed-bean-name> <managed-bean-class>jsfdemo.ApplicationInfoBean</managed-bean-class> <managed-bean-scope>application</managed-bean-scope> </managed-bean> Register the beans in faces-config.xml :
  • 50. Managed Beans – Example <h:outputFormat value=&quot;Hello, {0}&quot;> <f:param value=&quot;#{userBean.userName}&quot; /> </h:outputFormat> <h:outputFormat value=&quot;Application Info: {0}&quot;> <f:param value=&quot;#{applicationInfoBean.info}&quot; /> </h:outputFormat> <h:form id=&quot;userNameForm&quot;> <h:outputLabel for=&quot;userInput&quot; value=&quot;User Name:&quot; /> <h:inputText id=&quot;userInput&quot; value=&quot;#{userBean.userName}&quot; /> <h:commandButton value=&quot;Apply&quot; /> </h:form> <h:form id=&quot;appInfoForm&quot;> <h:outputLabel for=&quot;appInfoInput&quot; value=&quot;Application Info:&quot; /> <h:inputText id=&quot;appInfoInput&quot; value=&quot;#{userBean.applicationInfoBean.info}&quot; /> <h:commandButton value=&quot;Apply&quot; /> </h:form> Bind JSF controls with the beans:
  • 52. Simple JSF Sumator Using JSF binding and the Unified Expression Language and to sum numbers: <f:view> <h:form> <h:inputText value=&quot;#{firstNum}&quot; /> + <h:inputText value=&quot;#{secondNum}&quot; /> <h:commandButton value=&quot;OK&quot; /> </h:form> <h:outputFormat value=&quot; S um = {0}&quot; > <f:param value=&quot;#{firstNum + secondNum}&quot; /> </h:outputFormat> </f:view>
  • 53. Live Demo JSF Sumator
  • 55. What Is Navigation? Navigation is a set of rules for choosing the next page to be displayed Applied after a button or hyperlink is clicked The selection of the next page is determined by: The page that is currently displayed The action method invoked by the action property of the component that generated the event An outcome string that was returned by the action method or passed from the component
  • 56. Navigation Elements in faces-config.xml <from-view-id> element defines the source page. May be a pattern. For example /* . This will cause all JSF pages to redirect to some view on given outcome. <from-outcome> element defines the logical outcome as specified in the action attribute of the event source <to-view-id> element defines the page to be displayed when the specified outcome is returned <from-action> element refers to an action method that returns a String , which is the logical outcome
  • 57. Navigation Rules – Example <navigation-rule> <from-view-id> /* </from-view-id> <navigation-case> <from-outcome> home </from-outcome> <to-view-id> /navigation-demo.jsp </to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id> /navigation-demo.jsp </from-view-id> <navigation-case> <from-outcome> login_success </from-outcome> <to-view-id> /logged.jsp </to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id> /navigation-demo.jsp </from-view-id> <navigation-case> <from-outcome> login_failed </from-outcome> <to-view-id > /login-failed.jsp </to-view-id> </navigation-case> </navigation-rule>
  • 58. Action Attribute in JSF Form To specify what to be the form outcome you can Provide a constant string as action attribute of an event source Provide a managed bean method with no parameters which returns String Using this approach you can add some logic in this method that returns different result in different situations <h:commandButton value=&quot;Next Page&quot; action=&quot; nextPage &quot; /> <h:commandButton value=&quot;Login&quot; action=&quot; #{userBean.login} &quot; />
  • 60. Creating JSF Wizard Step 1 Enter first name, last name Step 2 Enter phone, email Final step Display all entered data We will use managed bean Person with session scope to store entered data We will use navigation rules for the active page
  • 61. Live Demo JSF Wizard
  • 63. Problems Create a simple JSF project which accepts your name and outputs it on the same page. Write a JSF based Web application which has several forms for entering information about a person. The first page must contain a form for entering name, age, country, town, occupation. The second page must contain a form for entering street address, town and country. The last page must show all collected information. Use JSF managed beans PersonInfo and Address for storing in the session all data entered in the forms.

Editor's Notes

  • #2: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #3: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #4: * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  • #7: * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## SPEL – Simplest Possible EL SEL – Simple EL JSP EL но Đľ подходящ - I mmediate evaluation , read-only Влиза в JSP - JSP EL иНи SEL (JSP 2.0) JSP&apos;s EL made retrieving values and resolving variables easier. Rather than using &lt;jsp:useBean&gt; and &lt;jsp:getProperty&gt;, a developer could simply use ${bean.property}
  • #8: * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  • #12: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #19: * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ## In the sixth phase of the lifecycle -- render response -- you display the view with all of its components in their current state.
  • #20: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #26: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #28: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #31: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #36: * 07/16/96 (c) 2006 National Academy for Software Development - http://academy.devbg.org* ##
  • #37: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #42: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #43: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #52: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #54: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #55: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #57: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## 10
  • #58: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## 10
  • #59: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## 10
  • #60: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #62: * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##