ak 4 jsf
DESCRIPTION
JSF classes by Atul Kahate Sir at SICSRTRANSCRIPT
What is JSF?
� JSF is a standard Java framework for building user interfaces for Web applications
� Created as a Java Community Process (JCP) –and hence has higher chances of being a
JavaServer Faces (JSF) | Atul Kahate 2
and hence has higher chances of being a success
� Attempts to make client-side development browser-independent and also includes features such as state management, etc
JSF Characteristics
� Server-side
� User-interface related
� Component framework� Component framework
� Components are UI-related, Validations-related, Error handling-related, etc
JavaServer Faces (JSF) | Atul Kahate 3
JSF Application Features
� Similar to JSP-servlet application
� Has a deployment descriptor (web.xml), JSP files, tag libraries, static resources, JSP files, tag libraries, static resources, etc
� See next slide
JavaServer Faces (JSF) | Atul Kahate 4
JSF Features Elaborated
� JSP pages: Form the UI
� Deployment descriptor: Indicates use of JSFJSF
� Swing-like enhanced UI
� Managed backing beans: Used to hold data entered by the user
� Superior validation
JavaServer Faces (JSF) | Atul Kahate 5
JSF and MVC
� Similar to Swing and other GUI frameworks:
� Model = Properties of an application (e.g. user name in an user object)
View = UI components that specify what events � View = UI components that specify what events occurred (e.g. value changed or button clicked)
� Controller = External event listeners that are attached to the UI to handle events
JavaServer Faces (JSF) | Atul Kahate 6
JSF Advantages
� Based on MVC
� Well-defined programming model and tag libraries
� Sun Standard
Helps reusability� Helps reusability
� UI development is easier, faster
� Event handling is easier
� Separates behavior and presentation of information
JavaServer Faces (JSF) | Atul Kahate 7
JSF Drawbacks
� Complex without the use of an IDE
� Still growing
� Applications without using MVC are tough to � Applications without using MVC are tough to build
� Confusion in file naming (pages are saved with .jsp but accessed as .jsf or .faces)
� Absence of regular expressions in validations
� Does not support GET method
JavaServer Faces (JSF) | Atul Kahate 8
JSF Parts
� JSF has three parts:
� Set of pre-fabricated User Interface componentscomponents
� Event-driven programming model
� Component model with the facility for allowing third-party components
JavaServer Faces (JSF) | Atul Kahate 9
JSF Application Lifecycle
� There are six phases
1. Restore view
2. Apply request values
3. Process validations3. Process validations
4. Update model values
5. Invoke application
6. Render response
� There are two types of requests
1. Initial request
2. Postback requests
JavaServer Faces (JSF) | Atul Kahate 10
JSF Request Types
1. Initial request
� Very first request for a page
� First time, so no UI processing/validations� First time, so no UI processing/validations
� Deals with Restore view and Render response phases
2. Postback request
� User has submitted a form
� All the six phases are dealt with hereJavaServer Faces (JSF) |
Atul Kahate 11
JSF Configuration
� A JSF application is a standard Java EE application, with the need for the following configuration files
Entry in the web.xml file Enables the Faces controller servlet when a URL pattern such as /faces/* is received
JavaServer Faces (JSF) | Atul Kahate 12
received
JSF configuration file faces-config.xml Allows for the configuration of the JSF application – at par with web.xml file, and is located in the WEB-INF/ folder
WEB-INF directory containing Actual JSF libraries jsf-api.jar and jsf-impl.jar
Apache commons libraries, such as commons-beanutils.jar, commns-collections.jar, commons-digester.jar, commons-logging.jar
JSTL jar files: jstl.jar and standard.jar
Converting a JSP to a JSF
� The first thing to do in order to convert a JSP page into a JSF page for better view is to add the following two taglibs:<%@ taglib uri=“http://java.sun.com/jsf/core” prefix=“f” %><%@ taglib uri=“http://java.sun.com/jsf/html” prefix=“h” %>
Then add a <f:view> tag in the body of the JSP
JavaServer Faces (JSF) | Atul Kahate 13
� Then add a <f:view> tag in the body of the JSP� This tag becomes the base UI component of the component
tree in memory of the server when the page is requested for viewing
� If the page also takes user input, we also need to add a <h:form> tag as a child element of the above tag� Subsequent HTML form element tags would be <h:inputText>, <h:commandButton>, etc
Example
<%@ page contentType="text/html" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %><%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view><html>
JavaServer Faces (JSF) | Atul Kahate 14
<html><body>
<h:form><h2>A simple JSF Page</h2>
<h:inputText value="#{modelBean.username}" /><h:commandButton value="Click here" />
</h:form></body>
</html></f:view>
JSF UI Component Tree – 1
� A JSF page like the above causes a UI component tree on the server, exactly matching with the components specified in the page
HtmlInputText HtmlCommandButton
JavaServer Faces (JSF) | Atul Kahate 15
HtmlInputText HtmlCommandButton
HtmlForm
UIViewRoot
JSF UI Component Tree – 2
� Once a tree like the above is created and loaded in the server’s memory, it is possible to interact with the server-side
JavaServer Faces (JSF) | Atul Kahate 16
possible to interact with the server-side UI components, and manipulate them directly
JSF Request Processing Lifecycle
� Sequence of events when a request is sent to a JSF page is called as the JSF request processing lifecycle, or simply JSF lifecycle
� Example:
JavaServer Faces (JSF) | Atul Kahate 17
� Example:<h:inputText value=“#{modelBean.username}” />
� This specifies that when the form is submitted, the value entered by the user in the input text box should be passed on to the corresponding property in the server-side JavaBean named modelBean
JSF Navigation Model – 1
View
(JSF pages)
JavaServer Faces (JSF) | Atul Kahate 18
Controller
(Faces servlet)
Model
(Managed beans)
JSF Navigation Model – 2
� Like Struts, JSF design is also based on an MVC approach
� Model is bound to methods and properties in the managed beans as specified in the faces-config.xml file
JavaServer Faces (JSF) | Atul Kahate 19
config.xml file� Controller is a servlet, that responds to all requests
that have a certain URL pattern, such as /faces/*, as defined in web.xml file� The controller prepares an object, called as JSF context,
which contains all accessible application data and routes the client to the appropriate view component (page)
� View is the set of JSF pages
JSF Navigation Model – 3
� The navigation model is based on a set of navigation rules
� Example
JavaServer Faces (JSF) | Atul Kahate 20
� Example� If something is a success then pass control
to something-1; else to something-2
� Achieved by specifying appropriate rules in the faces-config.xml file (See next slide)
JSF Navigation Model – 3
<navigation-rule><from-view-id>/page1.jsp</from-view-id><navigation-case>
<from-outcome>success</from-outcome>
JavaServer Faces (JSF) | Atul Kahate 21
<from-outcome>success</from-outcome><to-view-id>/page2.jsp</to-view-id>
</navigation-case><navigation-case>
<from-outcome>failure</from-outcome><to-view-id>/page3.jsp</to-view-id>
</navigation-case></navigation-rule>
JSF Case Study – Login
D:\Atul-personal\Lectures\SICSR\Web Technologies\WT-2\JSF\JSF-SimpleLogin (in NetBeans)
22JavaServer Faces (JSF) |
Atul Kahate
Application Logic
� The application should accept the user ID and password from the user and display a welcome page
JavaServer Faces (JSF) | Atul Kahate 23
display a welcome page
� The actual user ID and password logic would not be built in initially
index.jsp� <%@page contentType="text/html"%>� <%@page pageEncoding="UTF-8"%>
� <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>� <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
� <html>� <f:view>�
� <head>� <title>JSF Login Page Example</title>� </head>�
� <body bgcolor="pink">� <h:form>�
� <h1 align="center">The Login Page</h1>�
� <table border="1" bordecolor="maroon" cellspacing="1" cellpadding="1" align="center">
JavaServer Faces (JSF) | Atul Kahate 24
� <table border="1" bordecolor="maroon" cellspacing="1" cellpadding="1" align="center">�
� <tr>� <td>� <h:outputLabel for="txtName">� <h:outputText value="Name" />� </h:outputLabel>� </td>� <td><h:inputText id="txtName" value="#{UserBean.name}" /></td>� </tr>�
� <tr>� <td>� <h:outputLabel for="txtPassword">� <h:outputText value="Password" />� </h:outputLabel>� </td>� <td><h:inputSecret id="txtPassword" value="#{UserBean.password}" /></td>� </tr>�
� <tr align="center">� <td colspan="2">� <h:commandButton id="cmdLogin" value="Login" action="login" />� </td>� </tr>� </table>�
� </h:form>� </body>� </html>
� </f:view>
Understanding the JSP – 1
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
� These taglib directives refer to the JSTL tags
JavaServer Faces (JSF) | Atul Kahate 25
� These taglib directives refer to the JSTL tags
� jsf/core – Core library, contains custom action elements that represent JSF objects (which are independent of the page markup language)
� Jsf/html – HTML library, contains custom action elements that represent JSF objects (which are to be rendered as HTML elements)
Understanding the JSP – 2
<f:view>
� This is an action element� A view in JSF is the grouping of components
that make a specific UI screen
JavaServer Faces (JSF) | Atul Kahate 26
� A view in JSF is the grouping of components that make a specific UI screen
� The view contains an instance of the javax.faces.component.UIViewRoot class
� It does not display anything, but it is a container for other view components (e.g. input fields, buttons, etc)
Understanding the JSP – 3
<h:form>
� Represents a form component
JavaServer Faces (JSF) | Atul Kahate 27
Represents a form component
� Acts as a container for all input components that hold values that needs to be processed together
� Examples: <h:inputText>, <h:inputSecret>, <h:commandButton>
Understanding the JSP – 4
<h:outputLabel for="txtName">
<h:outputText value="Name" />
</h:outputLabel>
JavaServer Faces (JSF) | Atul Kahate 28
� Identifies a component that generates an HTML label
Understanding the JSP – 5
<h:inputText id="txtName" value="#{UserBean.name}" />
� Generates a text box with id txtName
JavaServer Faces (JSF) | Atul Kahate 29
� Generates a text box with id txtName
� The value that user enters here would populate an attribute called as name of a server-side JavaBean titled UserBean
Understanding the JSP – 6
<h:commandButton id="cmdLogin" value="Login" action="login" />
� Generates a command button with type
JavaServer Faces (JSF) | Atul Kahate 30
� Generates a command button with type as submit or reset
� The action attribute here has relevance, as explained later
How is this linked to the next page (welcome.jsp)?
index.jsp
…
<h:commandButton id … action = “login” />
…
faces-config.xml
JavaServer Faces (JSF) | Atul Kahate 31
faces-config.xml
…
<navigation-rule>
<from-view-id>/index.jsp<from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
…
welcome.jsp
…
<h:commandButton id … action = “login” />
…
1
2
3
UserBean.java� /*� * UserBean.java� *� * Created on September 25, 2007, 4:34 PM� *� * To change this template, choose Tools | Template Manager� * and open the template in the editor.� */
� package com.jsf.login;
� /**
JavaServer Faces (JSF) | Atul Kahate 32
� /**� *� * @author AtulK� */� public class UserBean {�
� private String name;� private String password;�
� public String getName() {� return name;� }�
� public void setName(String userName) {� name = userName;� }�
� public String getPassword() {� return password;� }�
� public void setPassword(String userPassword) {� password = userPassword;� }�
� }
Role of UserBean.javaindex.jsp
<html>
…
<td><h:inputText id = “txtName” value = “#{UserBean.name}” /></td>
…
Whatever the user enters on the
screen in the text box is passed to
the JavaBean’s set method
JavaServer Faces (JSF) | Atul Kahate 33
UserBean.java
public class UserBean {
private String name;
…
public String get.ame () {
return name;
}
public void set.ame (String userName) {
name = username;
}
…
}
welcome.jsp
� <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>� <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
� <html>� <f:view> � <head>� <title>Welcome to JSF!</title>
JavaServer Faces (JSF) | Atul Kahate 34
� <title>Welcome to JSF!</title>� </head>�
� <body>� <h:form>� <h1 align="center">� <h:outputText id="txtUserName" value="Welcome #{UserBean.name}!" />� </h1> � </h:form>�
� </body>� </f:view>� </html>
Managed Beans and Navigation
JavaServer Faces (JSF) | Atul Kahate 35
Managed Beans
� We know that the model in MVC is many times made up of JavaBeans
� A JavaBean in JSF is called as a � A JavaBean in JSF is called as a managed bean
JavaServer Faces (JSF) | Atul Kahate 36
Temperature Conversion
37JavaServer Faces (JSF) |
Atul Kahate
Requirement
� Accept temperature in Celsius and convert it into Fahrenheit
JavaServer Faces (JSF) | Atul Kahate 38
� D:\Atul-personal\Lectures\SICSR\Web Technologies\WT-2\JSF\TemperatureConversion (or in NetBeans 6.0 JSF-Temperature-Conversion)
index.jsp� <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>� <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
� <html>� <head>� <title>Celsius converter</title>� </head>� <body>� <f:view>� <h:form>� <table width="30%" height="30%" border="2" cellspacing="0" cellpadding="5">� <tr>� <td colspan="2">� <h:message for="celsiusEdit" />� </td>� </tr>
<tr>
JavaServer Faces (JSF) | Atul Kahate 39
� <tr>� <td>� <h:inputText id="celsiusEdit" value="#{pageBean.celsius}"/>� </td>� <td><b>� <h:outputText value="Celsius"/>� </b></td>� </tr>� <tr>� <td colspan="2">� <h:commandButton action="#{pageBean.convertToFahrenheit}" value="Convert"/>� </td>� </tr>� <tr>� <td>� <h:outputText value="#{pageBean.fahrenheit}"/>� </td>� <td><b>� <h:outputText value="Fahrenheit"/>� </b></td>� </tr>� </table>� </h:form>� </f:view>� </body>� </html>�
Understanding index.jsp – 1
<td colspan="2">
<h:message for="celsiusEdit" />
</td>
JavaServer Faces (JSF) | Atul Kahate 40
� Space reserved for possible error messages
Understanding index.jsp – 2
<td>
<h:inputText id="celsiusEdit“ value="#{pageBean.celsius}"/>
</td>
JavaServer Faces (JSF) | Atul Kahate 41
� Text box called as celsiusEdit would be created, which maps to the celsius property of the pageBean
Understanding index.jsp – 3
<td><b>
<h:outputText value="Celsius"/>
</b></td>
JavaServer Faces (JSF) | Atul Kahate 42
� HTML label would get created
Understanding index.jsp – 4
<td colspan="2">
<h:commandButtonaction="#{pageBean.convertToFahrenheit}" value="Convert"/>
</td>
JavaServer Faces (JSF) | Atul Kahate 43
</td>
� An HTML button would created, which would call the convertToFahrenheitmethod of the pageBean
Understanding index.jsp – 5
<td>
<h:outputText value="#{pageBean.fahrenheit}"/>
</td>
JavaServer Faces (JSF) | Atul Kahate 44
� Would display the value of the property fahrenheit of the pageBean
Understanding index.jsp – 6
<td><b>
<h:outputText value="Fahrenheit"/>
</b></td>
JavaServer Faces (JSF) | Atul Kahate 45
� Would create a label
faces-config.xml
� <?xml version="1.0"?>� <!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>
JavaServer Faces (JSF) | Atul Kahate 46
� <faces-config>� <managed-bean>� <description> � Simple backing bean.� </description> � <managed-bean-name>pageBean</managed-bean-name> � <managed-bean-class>com.iflex.PageBean</managed-bean-
class> � <managed-bean-scope>request</managed-bean-scope> � </managed-bean>� </faces-config>
pageBean.java� /*� * PageBean.java� *� * Created on September 20, 2007, 5:28 PM� *� * To change this template, choose Tools | Template Manager� * and open the template in the editor.� */
� package com.iflex;
� public class PageBean {� private Double celsius = null;� private Double fahrenheit = null;
� public PageBean(){System.out.println("PageBean()");
JavaServer Faces (JSF) | Atul Kahate 47
� System.out.println("PageBean()");� }
� public void setCelsius(Double celsius){� System.out.println("setCelsius");� this.celsius = celsius;� }
� public Double getCelsius(){� System.out.println("getCelsius");� return celsius;� }
� public void setFahrenheit(Double fahrenheit){� System.out.println("setFahrenheit");� this.fahrenheit = fahrenheit;� }
� public Double getFahrenheit(){� System.out.println("getFahrenheit");� return fahrenheit;� }
� public void convertToFahrenheit(){� System.out.println("convertToFahrenheit");� setFahrenheit(new Double(getCelsius().doubleValue() * 1.8 + 32));� }� }
Exercises
� Modify the temperature conversion example by reversing the logic (accept temperature in F, convert to C)
� Accept the number of dollars the user has got, and � Accept the number of dollars the user has got, and convert that into the equivalent Indian Rupees (USD 1 = INR 43.10)
� NetBeans USDToINR
� Accept two numbers from the user and tell the user which is greater among the two
� NetBeans USDToINR
JavaServer Faces (JSF) | Atul Kahate 48
Message Bundles
D:\Atul\Lectures\SICSR\Web Technologies\WT-2\JSF\JSF-More-
Examples
49JavaServer Faces (JSF) |
Atul Kahate
What are Message Bundles?
� When we implement a Web application, it is a good idea to collect all message strings in a central location
� Helps keeping messages consistent and also makes � Helps keeping messages consistent and also makes in application localization/internationalization easier
� Example
� indexWindowTitle=Hi there!
� thankYouWindowTitle=Thank you for submitting your information
JavaServer Faces (JSF) | Atul Kahate 50
Using Message Bundle – Step 1
� Add a properties file to your application (e.g. messages.properties file in Source packages -> com.corejsf)packages -> com.corejsf)
� indexWindowTitle=Using Textfields and Textareas
� thankYouWindowTitle=Thank you for submitting your information
� thankYouPageTitle=Thank You!
� indexPageTitle=Please enter the following personal information
� namePrompt=Name:
� submitPrompt=Submit your information
JavaServer Faces (JSF) | Atul Kahate 51
Using Message Bundle – Step 2
� Make references to properties defined earlier in your JSP as needed (index.jsp)(index.jsp)
� <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
� <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
� <%--
� This file is an entry point for JavaServer Faces application.
� --%>
� <html>
� <f:view>
� <head>
� <title>
� <h:outputText value = "#{msgs.indexWindowTitle}" />
� </title>
� </head>
� <body>
� <h1><h:outputText value="#{msgs.indexPageTitle}" /></h1>
�
� <h:form>
� <table>
� <tr>
� <td>
� <h:outputText value = "#{msgs.namePrompt}" />
� </td>
� <td>
� <h:inputText value = "#{user.name}" />
� </td>
� </tr>
� </table>
� <h:commandButton value="#{msgs.submitPrompt}" action="thankYou" />
� </h:form>
� </f:view>
� </body>
� </html>
JavaServer Faces (JSF) | Atul Kahate 52
Using Message Bundle – Step 3
� The other JSP (thankYou.jsp)� <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
� <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
� <html>
� <f:view>� <f:view>
� <head>
� <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
� <title>
� <h:outputText value = "#{msgs.thankYouWindowTitle}" />
� </title>
� </head>
� <body>
�
� <h1><h:outputText value="#{msgs.namePrompt}" />
� <h:outputText value="#{user.name}" /></h1>
�
� </f:view>
� </body>
� </html>
JavaServer Faces (JSF) | Atul Kahate 53
Using Message Bundle – Step 4
� Code the Bean (UserBean.java)� /*� * To change this template, choose Tools | Templates� * and open the template in the editor.� */
� package com.corejsf;
/**� /**� *� * @author atulk� */� public class UserBean {�
� private String name;
� public String getName() {� return name;� }
� public void setName(String name) {� this.name = name;� }
� }
JavaServer Faces (JSF) | Atul Kahate 54
Using Message Bundle – Step 5
� Add references to the config file (faces-config.xml)� <?xml version='1.0' encoding='UTF-8'?>
� <!-- =========== FULL CONFIGURATION FILE ================================== -->
� <faces-config version="1.2" � xmlns="http://java.sun.com/xml/ns/javaee" � xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" � xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">��
� <navigation-rule>� <from-view-id>/index.jsp</from-view-id>� <navigation-case>� <from-outcome>thankYou</from-outcome>� <to-view-id>/thankYou.jsp</to-view-id>� </navigation-case>� </navigation-rule>�
� <managed-bean>� <managed-bean-name>user</managed-bean-name>� <managed-bean-class>com.corejsf.UserBean</managed-bean-class>� <managed-bean-scope>session</managed-bean-scope> � </managed-bean>�
� <application>� <resource-bundle>� <base-name>com.corejsf.messages</base-name>� <var>msgs</var>� </resource-bundle>� </application>� </faces-config>
JavaServer Faces (JSF) | Atul Kahate 55
Various JSF UI-related Tags
D:\Atul\Lectures\SICSR\Web Technologies\WT-2\JSF\JSF-More-
Examples
JavaServer Faces (JSF) | Atul Kahate 56
index-UIExample.jsp
� <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
� <%@taglib prefix="h" � <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
� <html>
� <f:view>
� <head>
� <link href="styles.css"
JavaServer Faces (JSF) | Atul Kahate 57
showMoreInformation-UIExample.jsp
� <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
� <%@taglib prefix="h" � <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
� <html>
� <f:view>
� <head>
� <link href="styles.css"
JavaServer Faces (JSF) | Atul Kahate 58
faces-config.xml� <?xml version='1.0' encoding='UTF-8'?>
� <!-- =========== FULL CONFIGURATION FILE ================================== -->
� <faces-config version="1.2" � xmlns="http://java.sun.com/xml/ns/javaee" � xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" � xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">�
� <navigation-rule>� <from-view-id>/index.jsp</from-view-id>� <navigation-case>� <navigation-case>� <from-outcome>thankYou</from-outcome>� <to-view-id>/thankYou.jsp</to-view-id>� </navigation-case>� </navigation-rule>�
� <navigation-rule>� <from-view-id>/index-UIExample.jsp</from-view-id>� <navigation-case>� <from-outcome>showInformation</from-outcome>� <to-view-id>/showMoreInformation-UIExample.jsp</to-view-id>� </navigation-case>� </navigation-rule> �
� <managed-bean>� <managed-bean-name>user</managed-bean-name>� <managed-bean-class>com.corejsf.UserBean</managed-bean-class>� <managed-bean-scope>session</managed-bean-scope> � </managed-bean>�
� <managed-bean>� <managed-bean-name>form</managed-bean-name>� <managed-bean-class>com.corejsf.RegisterForm</managed-bean-class>� <managed-bean-scope>session</managed-bean-scope> � </managed-bean> �
� <application>� <resource-bundle>� <base-name>com.corejsf.messages</base-name>
JavaServer Faces (JSF) | Atul Kahate 59
RegisterForm.java� /*� * To change this template, choose Tools | Templates� * and open the template in the editor.� */� package com.corejsf;
� import java.text.DateFormatSymbols;� import java.util.*;
� import javax.faces.model.SelectItem;
� /**� /**� *� * @author atulk� */� public class RegisterForm {
� enum Education {� HIGH_SCHOOL, BACHELOR, MASTER, DOCTOR� };�
� private String name;� private boolean contactMe;� private Integer [] bestDaysToContact;� private Integer yearOfBirth;� private String [] colors;� private String [] languages;� private Education education;�
� public Integer [] getBestDaysToContact () {� return bestDaysToContact;� }
� public void setBestDaysToContact(Integer[] bestDaysToContact) {� this.bestDaysToContact = bestDaysToContact;� }
� public String[] getColors() {� return colors;� }
JavaServer Faces (JSF) | Atul Kahate 60
messages.properties� # To change this template, choose Tools | Templates� # and open the template in the editor.� indexWindowTitle=Using JSF UI Features� indexPageTitle=Please enter the following information
� namePrompt=Name:� contactMePrompt=Contact me� bestDayPrompt=What is the best day to contact you?� yearOfBirthPrompt=What year were you born?
buttonPrompt=Submit information� buttonPrompt=Submit information� languagePrompt=Select the languages you speak:� educationPrompt=Select your highest education level:� emailAppPrompt=Select your email application:� colorPrompt=Select your favourite colors:
� thankYouLabel=Thank you {0}, for your information� contactMeLabel=Contact me:� bestDayLabel=Best day to contact you:� yearOfBirthLabel=Your year of birth:� colorLabel=Colors:� languageLabel=Languages:� educationLabel=Education:
JavaServer Faces (JSF) | Atul Kahate 61
Messages
JavaServer Faces (JSF) | Atul Kahate 62
JSF Messages
� During the JSF life cycle, any object can create a message and add it to a queue of messages maintained by the faces context
At the end of the life cycle (i.e. in the Render � At the end of the life cycle (i.e. in the Render Response phase), we can display those messages in a view
� Usually, messages are associated with a UI component and are used to show validation errors
JavaServer Faces (JSF) | Atul Kahate 63
Message Types
� Information
� Warning
� Error� Error
� Fatal
JavaServer Faces (JSF) | Atul Kahate 64
Message Details
� All messages can contain a summary and a detail
� Example: summary could be Invalid entry and detail could be Age > 100 is not accepted
� Two JSF tags are used for message handling:� Two JSF tags are used for message handling:
� h:messages (Multiple messages for a component)
� h:message (Only a single message for a component)
� They have many attributes, such as errorClass, errorStyle, fatalClass, tooltip, etc
JavaServer Faces (JSF) | Atul Kahate 65
index-messageExample.jsp
� <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
� <%@taglib prefix="h" � <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
� <%--
� This file is an entry point for JavaServer Faces application.
� --%>JavaServer Faces (JSF) |
Atul Kahate 66
thankYou-messageExample.jsp
� <%--
� Document : thankYou-messageExamplemessageExample
� Created on : May 5, 2008, 3:26:50 PM
� Author : atulk
� --%>
� <%@page contentType="text/html"
JavaServer Faces (JSF) | Atul Kahate 67
AgeCheckBean.java
� /*
� * To change this template, choose Tools | TemplatesTools | Templates
� * and open the template in the editor.
� */
� package com.corejsf;
� /**
JavaServer Faces (JSF) | Atul Kahate 68
messages.properties
� # To change this template, choose Tools | Templates
� # and open the template in the editor.� # and open the template in the editor.
� greeting=Please fill out the following details
� indexWindowTitle=Using JSF UI Features
JavaServer Faces (JSF) | Atul Kahate 69
faces-config.xml
� <?xml version='1.0' encoding='UTF-8'?>
� <!-- =========== FULL CONFIGURATION FILE ================================== -->
� <faces-config version="1.2" JavaServer Faces (JSF) |
Atul Kahate 70
Panels
JavaServer Faces (JSF) | Atul Kahate 71
What are Panels?
� Normally we use HTML tables to align form prompts and messages
� It is error-prone and tedious� It is error-prone and tedious
� Hence, JSF introduces h:panelGrid, which generates a table element
<h:panelGrid columns=“3”>
…
</h:panelGrid>JavaServer Faces (JSF) |
Atul Kahate 72
Note about columns
� The columns attribute is optional – defaults to 1
� If specified, UI components are placed from left to right and top to bottomleft to right and top to bottom
� Example: If we specify columns as 3 and we have 9 components, we will get a panel grid with 3 rows and 3 columns – instead, if we have 10 components, we will have 4 rows and 3 columns (last two columns in the fourth row would be empty)
JavaServer Faces (JSF) | Atul Kahate 73
index-panelGrid.jsp
� <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
� <%@taglib prefix="h" � <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
� <%--
� This file is an entry point for JavaServer Faces application.
� --%>JavaServer Faces (JSF) |
Atul Kahate 74
faces-config.xml
� <?xml version='1.0' encoding='UTF-8'?>
� <!-- =========== FULL CONFIGURATION FILE ================================== -->
� <faces-config version="1.2" JavaServer Faces (JSF) |
Atul Kahate 75
Thank You!
76JavaServer Faces (JSF) |
Atul Kahate