lec5 ecom fall16_modified7_november16

70
e-Commerce Systems Zainab KHALLOUF, Ph.D. Lecture 5: JavaServer Faces (JSF) : Introduction and Overview

Upload: zainab-khallouf

Post on 27-Jan-2017

26 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Lec5 ecom fall16_modified7_november16

e-Commerce Systems

Zainab KHALLOUF, Ph.D.

Lecture 5: JavaServer Faces (JSF) : Introduction and Overview

Page 2: Lec5 ecom fall16_modified7_november16

Lecture References

The Java EE 7 Tutorial.

http://docs.oracle.com/javaee/7/tutorial/doc/javaeetutorial7.pdf

The figure in slide 4 is from: Mastering JavaServer Faces, by Bill Dudney, Jonathan

Lehr, Bill Willis, and LeRoy Mattingly.

JSF 2.0 lecture notes by Dr. Beatrice Amrhein, available from:

http://sws.bfh.ch/~amrhein/

E-Commerce Systems 2

Page 3: Lec5 ecom fall16_modified7_november16

JavaServer Faces (JSF) : Introduction

JavaServer Faces (JSF) is a user interface (GUI)component framework for developing Java webapplications.

JavaServer Faces UI components are configurable, reusableelements that compose the user interfaces of JavaServerFaces applications.A component can be simple, such as a button, or can becompound, such as a table, composed of multiplecomponents.(For more information about HTML Tag Library Tags, return to page 164 from :The Java EE 7 Tutorial).

E-Commerce Systems 3

Page 4: Lec5 ecom fall16_modified7_november16

JavaServer Faces (JSF) : Introduction (cont’)

JSF works in the web tier from the server-side.

JSF is the ”official” presentation technology in Java EE7standard.

JavaServer Faces technology APIs are layered directly ontop of the Servlet API.

E-Commerce Systems 4

Page 5: Lec5 ecom fall16_modified7_november16

JavaServer Faces (JSF) : Introduction (cont’)

JSF is a MVC (Model-View-Controller) framework. MVCis a design pattern that divides the application into threeinteroperable components:

Model: Classes that represent the business data and anybusiness logic that govern access to and modification of thedata.View: Pages that your application uses to dynamicallygenerate HTML responses. The view renders the contentsof a model. A view also forwards user inputs to a controller.Controller: Classes that handle incoming browser requests,and it defines application behavior. It selects views forpresentation. It interprets user inputs and maps them intoactions to be performed by the model. A controller selectsthe next view to display based on the user interactions andthe outcome of the model operations.

E-Commerce Systems 5

Page 6: Lec5 ecom fall16_modified7_november16

JavaServer Faces (JSF) : Introduction (cont’)

JSF is a MVC framework.

Controller: The JSF controller consists primarily of aservlet called FacesServlet.View: Represented by a set of web pages composed of a treeof UI components.Model:

Model objects referenced by the backing beans.The backing beans can include some controller functionslike page selection.

E-Commerce Systems 6

Page 7: Lec5 ecom fall16_modified7_november16

What Is a JavaServer Faces Application?

A typical JavaServer Faces application includes the followingparts:

1 A set of web pages, JSF uses JavaServer Pages (JSP) orFacelets (recommended) for its presentation technology.

JavaServer Pages (JSP) technology does not support all thenew features available in JSF in the Java EE 7 platform.

2 A set of backing beans (managed beans), which areJavaBeans components that define properties and functionsfor components on a page.

3 A web deployment descriptor (web.xml) file.4 Optionally, one or more application configuration resource

files, such as a faces-config.xml file, which can be used todefine user-defined navigation rules and configure beansand other custom objects, such as custom components.

5 Optionally, a set of custom objects, which can includecustom components, validators, converters, or listeners,created by the application developer.

E-Commerce Systems 7

Page 8: Lec5 ecom fall16_modified7_november16

What Is Facelets?

The term Facelets refers to the view declaration languagefor JavaServer Faces.

Facelets views are created as XHTML pages.

JSF EL expression use #{. . . }, while JSP EL expressionsuse the ${. . . } syntax.

Return to page 130 from:

The Java EE 7 Tutorial.

http://docs.oracle.com/javaee/7/tutorial/doc/javaeetutorial7.pdf

E-Commerce Systems 8

Page 9: Lec5 ecom fall16_modified7_november16

The tag libraries supported by Facelets

JavaServer Faces technology supports various tag libraries to addcomponents to a web page. To support the JavaServer Faces taglibrary mechanism, Facelets uses XML namespace declarations.

E-Commerce Systems 9

Page 10: Lec5 ecom fall16_modified7_november16

The tag libraries supported by Facelets (cont’)

E-Commerce Systems 10

Page 11: Lec5 ecom fall16_modified7_november16

The tag libraries supported by Facelets (cont’)

Example of using XML namespace declarations in Facelets.

E-Commerce Systems 11

Page 12: Lec5 ecom fall16_modified7_november16

The interaction between client and server in a typicalJSF application

The Java EE 6 Tutorial.

http://download.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf

E-Commerce Systems 12

Page 13: Lec5 ecom fall16_modified7_november16

JavaServer Faces (JSF) : Introduction (cont’)

To use JSF you need:

JSF Implementation (in the form of the JSF jars).

The JavaServer Pages Standard Tag Library (JSTL) tagslibrary.

A Java runtime environment.

A web-container to use JSF in (for example Tomcat).

E-Commerce Systems 13

Page 14: Lec5 ecom fall16_modified7_november16

Some existing JSF Implementations

Sun implementation (Mojarra) (open source) used inmany ASs.

JBoss RichFaces (open source).

Primefaces, (open source).

Apache MyFaces, (open source).

E-Commerce Systems 14

Page 15: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using NetBeans IDE

Page 16: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using an IDE, such asNetBeans

E-Commerce Systems 16

Page 17: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using NetBeans (cont’)

E-Commerce Systems 17

Page 18: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using NetBeans (cont’)

E-Commerce Systems 18

Page 19: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using NetBeans (cont’)

E-Commerce Systems 19

Page 20: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using NetBeans (cont’)

E-Commerce Systems 20

Page 21: Lec5 ecom fall16_modified7_november16

Creating a JSF Application Using NetBeans (cont’)

E-Commerce Systems 21

Page 22: Lec5 ecom fall16_modified7_november16

Examples

Page 23: Lec5 ecom fall16_modified7_november16

Hello World in JSF!

Page 24: Lec5 ecom fall16_modified7_november16

Creating a Simple JavaServer Faces Application

1 Developing backing beans.

2 Adding managed bean annotations (declaration).

3 Creating web pages using Facelets or JSP tags.

4 Mapping the FacesServlet instance.

E-Commerce Systems 24

Page 25: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean

E-Commerce Systems 25

Page 26: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean

E-Commerce Systems 26

Page 27: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean

E-Commerce Systems 27

Page 28: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean

E-Commerce Systems 28

Page 29: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean (cont’)

The @Named annotation makes the managed beanaccessible through the EL.

You can specify an argument to the @Named to use anondefault name.

Example : @Named(”Hello”). With this annotation, theFacelets page would refer to the bean as Hello.

Managed beans are created and initialized the first timethey are requested by an EL expression.

The @RequestScoped annotation controls the lifespan ofthe managed bean. Managed beans are automaticallyinstantiated and registered in their scope.

Managed beans should be specified as serializable.

E-Commerce Systems 29

Page 30: Lec5 ecom fall16_modified7_november16

Creating the Web Page

index.xhtml, is a simple XHTML page. It has the followingcontent:

The web page connects to the backing bean through theExpression Language (EL) value expression #{hello.world},which retrieves the value of the world property from thebacking bean Hello.

E-Commerce Systems 30

Page 31: Lec5 ecom fall16_modified7_november16

Mapping the FacesServlet Instance

Done through the web deployment descriptor (web.xml),automatically done for you if you are using an IDE such asNetBeans IDE.

E-Commerce Systems 31

Page 32: Lec5 ecom fall16_modified7_november16

Libraries

E-Commerce Systems 32

Page 33: Lec5 ecom fall16_modified7_november16

Result

E-Commerce Systems 33

Page 34: Lec5 ecom fall16_modified7_november16

Example 2

Page 35: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean

package bean;

...

@RequestScoped

@Named

public class HelloBean implements Serializable{

private String name;

private int age;

private String helloText;

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getHelloText() {

return helloText;

}

....

....

/** Define the text to say hello. */

public String sayHello() {

if (age < 11) {

helloText = "Hello " + name + "!";

} else {

helloText = "Good Morning " + name + "!";

}

return ("home");

}

}

E-Commerce Systems 35

Page 36: Lec5 ecom fall16_modified7_november16

Creating the Web Pages

index.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>Facelet Title</title>

</h:head>

<h:body>

<h:form id="main">

<h:outputText value="Hello World" styleClass="header" />

<table>

<tr>

<td><h:outputLabel for="helloInput" value="Your Name" /></td>

<td><h:inputText id="helloInput"

value="#{helloBean.name}" required="true" /></td>

</tr>

<tr>

<td><h:outputLabel for="helloAge" value="Your Age" /></td>

<td><h:inputText id="helloAge"

value="#{helloBean.age}" /></td>

</tr>

</table><p />

<h:commandButton value="Say Hello"

action="#{helloBean.sayHello}" />

</h:form>

</h:body>

</html>

E-Commerce Systems 36

Page 37: Lec5 ecom fall16_modified7_november16

Creating the Web Pages (cont’)

home.xhtml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<title>TODO supply a title</title>

</head>

<body>

<p>

#{helloBean.helloText}

</p>

</body>

</html>

E-Commerce Systems 37

Page 38: Lec5 ecom fall16_modified7_november16

Example 3 : Guessing Game

Page 39: Lec5 ecom fall16_modified7_november16

Developing the Backing Bean

package bean;

...

@SessionScoped

@Named

public class GuessBean implements Serializable{

private int target;

private int guess = -1;

private int attempts;

private String message;

public GuessBean() {

target = (int) (Math.random() * 100);

}

//Setters and getters

....

....

public String guessCommand() {

if (guess == target) {

FacesContext context = FacesContext.getCurrentInstance();

HttpSession session = (HttpSession) context.getExternalContext().getSession(false);

session.invalidate();

return ("sucess");

} else {

this.attempts++;

this.message = "Your guess is incorrect, guess again please";

return ("index");

}

}

}

E-Commerce Systems 39

Page 40: Lec5 ecom fall16_modified7_november16

Creating the Web Pages

index.xhtml

...

<h:form>

<h:inputText id="guess" value="#{guessBean.guess}">

<f:validateLongRange minimum="0" maximum="100"/>

</h:inputText>

<h:message showSummary="true" showDetail="false"

styleClass="error"

for="guess"/>

<h:commandButton id="cmd" value="Try" action="#{guessBean.guessCommand}"/>

<br/>

<h:outputText id="msg" value="#{guessBean.message}"

rendered="#{guessBean.attempts gt 0}"/>

<br/>

<h:outputText id="attem" value="Attempts: #{guessBean.attempts}"

rendered="#{guessBean.attempts gt 0}"/>

</h:form>

...

f:validateLongRange : A standard validator tag to validatethe input value.

rendered : Specifies a condition under which thecomponent should be rendered. If the condition is notsatisfied, the component is not rendered.

E-Commerce Systems 40

Page 41: Lec5 ecom fall16_modified7_november16

Creating the Web Pages (cont’)

sucess.xhtml

...

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<title>Congratulations</title>

</head>

<body>

<p>

Congratulations

</p>

</body>

</html>

E-Commerce Systems 41

Page 42: Lec5 ecom fall16_modified7_november16

Example 4 : A simple application to illustrate the use ofentities, facade session bean and JSF.

Page 43: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

E-Commerce Systems 43

Page 44: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

Account.java

...

@Entity

public class Account implements Serializable {

private static final long serialVersionUID = 1L;

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;

private String ownerName;

private int balance;

public Account() {

}

...

//setters and getters

public void deposit(int amount) {

balance += amount;

}

public int withdraw(int amount) {

if (amount > balance) {

return 0;

} else {

balance -= amount;

return amount;

}

}

}

E-Commerce Systems 44

Page 45: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

AccountFacade.java

...

@Stateless

public class AccountFacade {

@PersistenceContext(unitName = "BankEntityWebApplicationPU")

private EntityManager em;

public Account openAccount(String ownerName, int balance) {

Account account = new Account();

account.setOwnerName(ownerName);

account.setBalance(balance);

em.persist(account);

return account;

}

public void deposit(int accountNumber, int amount) {

Account account = em.find(Account.class, accountNumber);

account.deposit(amount);

}

public int withdraw(int accountNumber, int amount) {

Account account = em.find(Account.class, accountNumber);

return account.withdraw(amount);

}

public void close(int accountNumber) {

Account account = em.find(Account.class, accountNumber);

em.remove(account);

}

public List<Account> listAccounts() {

Query query = em.createQuery("SELECT a FROM Account a");

return query.getResultList();

}

}

}

E-Commerce Systems 45

Page 46: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

AccountInfoView.java

...

@RequestScoped

@Named

public class AccountInfoView implements Serializable{

@EJB

private AccountFacade accountFacade;

private Account account;

public List<Account> getAccounts() {

return (accountFacade.listAccounts());

}

public Account getAccount() {

return account;

}

public void setAccount(Account account) {

this.account = account;

}

public AccountInfoView() {

this.account = new Account();

}

public void SetAccountInfo(String ownername, int balance) {

account.setOwnerName(ownername);

account.setBalance(balance);

}

public String creatAccount() {

this.accountFacade.openAccount(account.getOwnerName(), account.getBalance());

return "response";

}

}

E-Commerce Systems 46

Page 47: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

index.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"

xmlns:f="http://java.sun.com/jsf/core">

<h:head>

<title>Facelet Title</title>

</h:head>

<h:body>

<f:view>

<h:form>

<h:outputLabel value="Client Name:"/>

<h:inputText value="#{accountInfoView.account.ownerName}"/>

<h:outputLabel value="Balance:"/>

<h:inputText value="#{accountInfoView.account.balance}"/>

<h:commandButton action="#{accountInfoView.creatAccount}" value="Create Account"/>

</h:form>

</f:view>

</h:body>

</html>

E-Commerce Systems 47

Page 48: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

response.xhtml : Using h:dataTable.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

</head>

<body>

<p>

<h:dataTable var="acc"

value="#{accountInfoView.accounts}"

border="1">

<h:column>

<f:facet name="header">Owner Name</f:facet>

#{acc.ownerName}

</h:column>

<h:column>

<f:facet name="header">Balance</f:facet>

#{acc.balance}

</h:column>

</h:dataTable>

</p>

</body>

</html>

E-Commerce Systems 48

Page 49: Lec5 ecom fall16_modified7_november16

Example 4 (cont.)

response.xhtml : Using ui:repeat.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

</head>

<body>

<p>

<ul>

<ui:repeat var="acc" value="#{accountInfoView.accounts}">

<li> #{acc.ownerName} , #{acc.balance}</li>

</ui:repeat>

</ul>

</p>

</body>

</html>

E-Commerce Systems 49

Page 50: Lec5 ecom fall16_modified7_november16

Generating JSF pages for Entities in Netbeans

E-Commerce Systems 50

Page 51: Lec5 ecom fall16_modified7_november16

Templating

Page 52: Lec5 ecom fall16_modified7_november16

Templating

Templating is a useful Facelets feature that allows you tocreate a page that will act as the base, or template, for theother pages in an application.

By using templates, you can reuse code and avoidrecreating similarly constructed pages.

The NetBeans IDE provides a Facelets Template wizard forcreating Facelets templates, and a Facelets Template Clientwizard for creating files that rely on a template.

The Java EE 6 Tutorial.

E-Commerce Systems 52

Page 53: Lec5 ecom fall16_modified7_november16

Basic Steps to Use Templating

1 Creating template file

Content that will appear in all clients is entered directly.Content that can be replaced in client files is marked withui:insert (with default values in case client does not supplycontent).

2 Creating template client files

Use ui:composition to specify the template file used.Use ui:define to override content for each replaceable sectionin template (marked in template with ui:insert).

http://www.coreservlets.com/JSF-Tutorial/jsf2/

E-Commerce Systems 53

Page 54: Lec5 ecom fall16_modified7_november16

Templating Example

E-Commerce Systems 54

Page 55: Lec5 ecom fall16_modified7_november16

Templating Example (cont’)

E-Commerce Systems 55

Page 56: Lec5 ecom fall16_modified7_november16

Templating Example (cont’)

Creating Template File:

E-Commerce Systems 56

Page 57: Lec5 ecom fall16_modified7_november16

Templating Example (cont’)

Creating Template Client File:

E-Commerce Systems 57

Page 58: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup

Page 59: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup

JavaServer Faces technology supports HTML5 by allowingto use HTML5 markup directly. It also allows you to useHTML5 attributes within JavaServer Faces elements.

JavaServer Faces technology support for HTML5 falls intotwo categories:

Pass-through elements.Pass-through attributes.

Return to page 145 from:

The Java EE 7 Tutorial.

http://docs.oracle.com/javaee/7/tutorial/doc/javaeetutorial7.pdf

E-Commerce Systems 59

Page 60: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: Pass-through elements

Pass-through elements allow you to use HTML5 tags andattributes and to treat them as equivalent to JavaServerFaces components.

To make a non-JavaServer Faces element a pass-throughelement, specify at least one of its attributes using thehttp://xmlns.jcp.org/jsf namespace. For example, thefollowing code declares the namespace with the short namejsf:

<input type="email" jsf:id="email" name="email" value="#{bean.email}" required="required"/>

Here, the jsf prefix is placed on the id attribute so that theHTML5 input tag’s attributes are treated as part of theFacelets page. This means that, for example, you can useEL expressions to retrieve managed bean properties.

E-Commerce Systems 60

Page 61: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: Using Pass-throughAttributes

They allow you to pass non-JavaServer Faces attributes.There are several ways to specify pass-through attributes,in his lecture we will focus on one method:

Use the JavaServer Faces namespace:xmlns:p=”http://xmlns.jcp.org/jsf/passthrough, to prefixthe attribute names within a JavaServer Faces component.Example : The following code passes the type, min, max,required, and title attributes through to the JSF inputcomponent:

<html ... xmlns:p="http://xmlns.jcp.org/jsf/passthrough"

...

<h:inputText id="nights" p:type="number" value="#{bean.nights}"

p:min="1" p:max="30"

p:required="required"

p:title="Enter a number between 1 and 30 inclusive."/>

E-Commerce Systems 61

Page 62: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication

The reservation example application simulates purchasingtickets. It consists of two Facelets pages, reservation.xhtml andconfirmation.xhtml, and a backing bean, Bean.java. The pagesuse both pass-through attributes and pass-through elements.

The Java EE 7 Tutorial.

http://docs.oracle.com/javaee/7/tutorial/doc/javaeetutorial7.pdf

E-Commerce Systems 62

Page 63: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

E-Commerce Systems 63

Page 64: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

E-Commerce Systems 64

Page 65: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

E-Commerce Systems 65

Page 66: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

reservation.xhtml

...

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:f="http://xmlns.jcp.org/jsf/core"

xmlns:h="http://xmlns.jcp.org/jsf/html"

xmlns:p="http://xmlns.jcp.org/jsf/passthrough"

xmlns:jsf="http://xmlns.jcp.org/jsf">

<h:head>

</h:head>

<h:form prependId="false">

<p>

<label>Name (First Last):</label>

<br/>

<input type="text" jsf:id="full_name" name="full_name"

placeholder="John Doe"

value="#{bean.name}" required="required"/>

</p>

<p>

<label>Email:</label>

<br/>

<input type="email" jsf:id="email" name="email" value="#{bean.email}"

required="required"/>

</p>

E-Commerce Systems 66

Page 67: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

reservation.xhtml (cont’)

<p>

<label>Date Of Arrival:</label>

<br/>

<input type="date" jsf:id="arrival" name="arrival" value="#{bean.arrival}"

required="required"/>

</p>

<p>

<label>Number Of Nights:</label>

<br/>

<h:inputText id="nights" p:type="number" value="#{bean.nights}"

p:min="1" p:max="30" p:required="required"

p:title="Enter a number between 1 and 30 inclusive."/>

</p>

<p>

<label>Number Of Guests:</label>

<br/>

<input jsf:id="guests" type="number" value="#{bean.guests}" min="1"

max="4" required="required"

title="Enter a number between 1 and 4 inclusive."/>

</p>

<p>

<h:commandButton value="Make Reservation" action="#{bean.calculateTotal}"/>

</p>

</h:form>

</html>

E-Commerce Systems 67

Page 68: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

confirmation.xhtml

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:h="http://xmlns.jcp.org/jsf/html"

xmlns:jsf="http://xmlns.jcp.org/jsf">

<h:head>

</h:head>

<h:outputStylesheet name="stylesheet.css" />

<h:form>

<output jsf:id="confirm" name="confirm">

Thanks for your reservation, #{bean.name}. #{bean.guests} guests are

confirmed for #{bean.arrival}. A fictitous email has been sent to #{bean.email}.

<p>

<label>Estimated Total:</label>

<br/>

<output jsf:id="total" name="total">#{bean.totalValue}</output>

</p>

</output>

<p>

<h:commandButton value="Back" action="reservation"/>

</p>

</h:form>

</html>

E-Commerce Systems 68

Page 69: Lec5 ecom fall16_modified7_november16

HTML5 Friendly Markup: The reservation ExampleApplication (cont’)

Bean.java

...

@RequestScoped

@Named

public class Bean implements Serializable {

private String name = "";

private String totalValue = "";

private String email = "";

private String arrival = "";

private String nights = "1";

private String guests = "1";

....

public String calculateTotal() {

int nightsNum = 0;

int guestsNum = 1;

int total = 0;

if (nights.trim().length() > 0) {

nightsNum = Integer.parseInt(nights);

}

if (guests.trim().length() > 0) {

guestsNum = Integer.parseInt(guests);

}

total = (nightsNum * 99) + ((guestsNum - 1) * 10);

totalValue = String.valueOf(total) + ".00";

return "confirmation";

}

}E-Commerce Systems 69

Page 70: Lec5 ecom fall16_modified7_november16

Questions?