Professional Documents
Culture Documents
A JavaServer Pages component is a type of Java servlet that is designed to fulfill the
role of a user interface for a Java web application. Web developers write JSPs as text
files that combine HTML or XHTML code, XML elements, and embedded JSP actions
and commands.
Using JSP, you can collect input from users through Webpage forms, present records
from a database or another source, and create Webpages dynamically.
JSP tags can be used for a variety of purposes, such as retrieving information from a
database or registering user preferences, accessing JavaBeans components, passing
control between pages, and sharing information between requests, pages etc.
JSP are always compiled before they are processed by the server unlike CGI/Perl
which requires the server to load an interpreter and the target script each time
the page is requested.
JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP
also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI,
EJB, JAXP, etc.
JSP pages can be used in combination with servlets that handle the business
logic, the model supported by Java servlet template engines.
Finally, JSP is an integral part of Java EE, a complete platform for enterprise class
applications. This means that JSP can play a part in the simplest applications to the
most complex and demanding.
Advantages of JSP
Following table lists out the other advantages of using JSP over other technologies −
vs. JavaScript
JavaScript can generate HTML dynamically on the client but can hardly interact with
the web server to perform complex tasks like database access and image processing
etc.
1. Model 1 Architecture
2. Model 2 (MVC) Architecture
Model 1 Architecture
Servlet and JSP are the main technologies to develop the web applications.
Servlet was considered superior to CGI. Servlet technology doesn't create process, rather it
creates thread to handle request. The advantage of creating thread over process is that it
doesn't allocate separate memory area. Thus many subsequent requests can be easily handled
by servlet.
JSP overcomes almost all the problems of Servlet. It provides better separation of concern,
now presentation and business logic can be easily separated. You don't need to redeploy the
application if JSP page is modified. JSP provides support to develop web application using
JavaBean, custom tags and JSTL so that we can put the business logic separate from our JSP
that will be easier to test and debug.
As you can see in the above figure, there is picture which show the flow of the model1
architecture.
Model The model represents the state (data) and business logic of the application.
View The view module is responsible to display data i.e. it represents the presentation.
Controller The controller module acts as an interface between view and model. It intercepts all
the requests i.e. receives input and commands to Model / View to change accordingly.
What is MVC?
MVC is an architecture that separates business logic, presentation and data.
In MVC,
MVC is a systematic way to use the application where the flow starts from the
view layer, where the request is raised and processed in controller layer and
sent to model layer to insert data and get back the success or failure
message.
Model Layer:
This is the data layer which consists of the business logic of the system.
It consists of all the data of the application
It also represents the state of the application.
It consists of classes which have the connection to the database.
The controller connects with model and fetches the data and sends to
the view layer.
The model connects with the database as well and stores the data into a
database which is connected to it.
View Layer:
Controller Layer:
It acts as an interface between View and Model.
It intercepts all the requests which are coming from the view layer.
It receives the requests from the view layer and processes the requests
and does the necessary validation for the request.
This requests is further sent to model layer for data processing, and
once the request is processed, it sends back to the controller with
required information and displayed accordingly by the view.
We are taking the example of a form with two variables "email" and
"password" which is our view layer.
Once the user enters email, and password and clicks on submit then the
action is passed in mvc_servlet where email and password are passed.
This mvc_servlet is controller layer. Here in mvc_servlet the request is
sent to the bean object which act as model layer.
The email and password values are set into the bean and stored for
further purpose.
From the bean, the value is fetched and shown in the view layer.
What is JSF?
JavaServer Faces (JSF) is a MVC web framework that simplifies the construction of
User Interfaces (UI) for server-based applications using reusable UI components in a
page. JSF provides a facility to connect UI widgets with data sources and to server-
side event handlers. The JSF specification defines a set of standard UI components
and provides an Application Programming Interface (API) for developing components.
JSF enables the reuse and extension of the existing standard UI components.
Benefits
JSF reduces the effort in creating and maintaining applications, which will run on a
Java application server and will render application UI on to a target client. JSF
facilitates Web application development by −
Create Project
Let's open command console, go the C:\ > JSF directory and execute the
following mvn command.
C:\JSF>mvn archetype:create
-DgroupId = com.tutorialspoint.test
-DartifactId = helloworld
-DarchetypeArtifactId = maven-archetype-webapp
Maven will start processing and will create the complete java web application project
structure.
Now go to C:/JSF directory. You'll see a Java web application project created, named
helloworld (as specified in artifactId). Maven uses a standard directory layout as
shown in the following screenshot.
Using the above example, we can understand the following key concepts.
1 helloworld
2 src/main/wepapp
3 src/main/resources
It contains images/properties files (In the above example, we need to
create this structure manually)
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.7</version>
</dependency>
</dependencies>
Complete POM.xml
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.test</groupId>
<artifactId>helloworld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.7</version>
</dependency>
</dependencies>
<build>
<finalName>helloworld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven will start processing, create the eclipse ready project, and will add wtp
capability.
Downloading: http://repo.maven.apache.org/org/apache/maven/plugins/
maven-compiler-plugin/2.3.1/maven-compiler-plugin-2.3.1.pom
5K downloaded (maven-compiler-plugin-2.3.1.pom)
Downloading: http://repo.maven.apache.org/org/apache/maven/plugins/
maven-compiler-plugin/2.3.1/maven-compiler-plugin-2.3.1.jar
29K downloaded (maven-compiler-plugin-2.3.1.jar)
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------
[INFO] Building helloworld Maven Webapp
[INFO] task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse {execution: default-cli}]
[INFO] Adding support for WTP version 2.0.
[INFO] Using Eclipse Workspace: null
[INFO] Adding default classpath container: org.eclipse.jdt.
launching.JRE_CONTAINER
Downloading: http://repo.maven.apache.org/
com/sun/faces/jsf-api/2.1.7/jsf-api-2.1.7.pom
12K downloaded (jsf-api-2.1.7.pom)
Downloading: http://repo.maven.apache.org/
com/sun/faces/jsf-impl/2.1.7/jsf-impl-2.1.7.pom
10K downloaded (jsf-impl-2.1.7.pom)
Downloading: http://repo.maven.apache.org/
com/sun/faces/jsf-api/2.1.7/jsf-api-2.1.7.jar
619K downloaded (jsf-api-2.1.7.jar)
Downloading: http://repo.maven.apache.org/
com/sun/faces/jsf-impl/2.1.7/jsf-impl-2.1.7.jar
1916K downloaded (jsf-impl-2.1.7.jar)
[INFO] Wrote settings to C:\JSF\helloworld\.settings\
org.eclipse.jdt.core.prefs
[INFO] Wrote Eclipse project for "helloworld" to C:\JSF\helloworld.
[INFO]
[INFO] -----------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -----------------------------------------------------------
[INFO] Total time: 6 minutes 7 seconds
[INFO] Finished at: Mon Nov 05 16:16:25 IST 2012
[INFO] Final Memory: 10M/89M
[INFO] -----------------------------------------------------------
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id = "WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>faces/home.xhtml</welcome-file>
</welcome-file-list>
<!--
This servlet initializes the JSF components before the JSP is displayed.
-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
package com.tutorialspoint.test;
import javax.faces.bean.ManagedBean;
public HelloWorld() {
System.out.println("HelloWorld started!");
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>JSF Tutorial!</title>
</head>
<body>
#{helloWorld.getMessage()}
</body>
</html>
Maven will start building the project and will create helloworld.war under C:\ →
Projects → Data → WorkSpace → helloworld → target folder.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
[INFO] [war:war]
[INFO] Packaging webapp
[INFO] Assembling webapp[helloworld] in
[C:\Projects\Data\WorkSpace\helloworld\target\helloworld]
[INFO] Processing war project
[INFO] Webapp assembled in[150 msecs]
[INFO] Building war:
C:\Projects\Data\WorkSpace\helloworld\target\helloworld.war
[INFO] ------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Mon Nov 05 16:34:46 IST 2012
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------
Look inside webapps directory, there should be a folder helloworld got created.
Run Application
Enter a url in web browser: http://localhost:8080/helloworld/home.jsfto launch
the application.
Server name (localhost) and port (8080) may vary as per your tomcat configuration.
JSF User Interface Component Model
JavaServer Faces provides rich set of components library to define the architecture of
application.
Rich set of classes for specifying the state and behavior of user interface components.
o A rendering model that defines how to render the components in various ways.
o A conversion model that defines how to register data converters onto a component.
o An event and listener model that defines how to handle component events.
o A validation model that defines how to register validators onto a component.
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd">
2. <html xmlns="http://www.w3.org/1999/xhtml"
3. xmlns:h="http://xmlns.jcp.org/jsf/html"
4. xmlns:f="http://xmlns.jcp.org/jsf/core">
5. <h:head>
6. <title>User Registration Form</title>
7. </h:head>
8. <h:body>
9. <h:form id="form">
10. <table>
11. <tr>
12. <td><h:outputLabel for="username">User Name</h:outputLabel></td>
13. <td><h:inputText id="name-id" value="#{user.name}"/></td>
14. </tr>
15. <tr>
16. <td><h:outputLabel for="email">Your Email</h:outputLabel></td>
17. <td><h:inputText id="email-id" value="#{user.email}"/></td>
18. </tr>
19. <tr>
20. <td><h:outputLabel for="password">Password</h:outputLabel></td>
21. <td><h:inputSecret id="password-id" value="#{user.password}"/></td>
22. </tr>
23.
24. <tr>
25. <td><h:outputLabel for="gender">Gender</h:outputLabel></td>
26. <td><h:selectOneRadio value="#{user.gender}">
27. <f:selectItem itemValue="Male" itemLabel="Male" />
28. <f:selectItem itemValue="Female" itemLabel="Female" />
29. </h:selectOneRadio></td>
30. </tr>
31. <tr><td><h:outputLabel for="address">Address</h:outputLabel></td>
32. <td><h:inputTextarea value="#{user.address}" cols="50" rows="5"/></td></tr>
33. </table>
34. <h:commandButton value="Submit" action="response.xhtml"></
h:commandButton>
35. </h:form>
36. </h:body>
37. </html>
1. import javax.faces.bean.ManagedBean;
2. import javax.faces.bean.RequestScoped;
3. @ManagedBean
4. @RequestScoped
5. public class User{
6. String name;
7. String email;
8. String password;
9. String gender;
10. String address;
11. public String getName() {
12. return name;
13. }
14. public void setName(String name) {
15. this.name = name;
16. }
17. public String getEmail() {
18. return email;
19. }
20.
21. public void setEmail(String email) {
22. this.email = email;
23. }
24. public String getPassword() {
25. return password;
26. }
27. public void setPassword(String password) {
28. this.password = password;
29. }
30. public String getGender() {
31. return gender;
32. }
33. public void setGender(String gender) {
34. this.gender = gender;
35. }
36. public String getAddress() {
37. return address;
38. }
39. public void setAddress(String address) {
40. this.address = address;
41. }
42. }
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd">
2. <html xmlns="http://www.w3.org/1999/xhtml"
3. xmlns:h="http://xmlns.jcp.org/jsf/html"
4. xmlns:f="http://xmlns.jcp.org/jsf/core">
5. <h:head>
6. <title>User Details</title>
7. </h:head>
8. <h:body>
9. <h2><h:outputText value="Hello #{user.name}"/></h2>
10. <h4><h:outputText value="You have Registered with us Successfully, Your Details ar
e The Following."/></h4>
11. <table>
12. <tr>
13. <td><b>Email: </b></td>
14. <td><h:outputText value="#{user.email}"/><br/></td>
15. </tr>
16. <tr>
17. <td><b>Password:</b></td>
18. <td><h:outputText value="#{user.password}"/><br/></td>
19. </tr>
20. <tr>
21. <td><b>Gender:</b></td>
22. <td><h:outputText value="#{user.gender}"/><br/></td>
23. </tr>
24. <tr>
25. <td><b>Address: </b></td>
26. <td><h:outputText value="#{user.address}"/></td>
27. </tr>
28. </table>
29. </h:body>
30. </html>
After submitting form, JSF renders response.xhtml file as a result web page.
// response page
Navigation rules can be defined in JSF configuration file named faces-config.xml. They
can be defined in managed beans.
Navigation rules can contain conditions based on which the resulted view can be
shown. JSF 2.0 provides implicit navigation as well in which there is no need to define
navigation rules as such.
Implicit Navigation
JSF 2.0 provides auto view page resolver mechanism named implicit navigation.
In this case, you only need to put view name in action attribute and JSF will search the
correct view page automatically in the deployed application.
<h:form>
</h:form>
@RequestScoped
return "page1";
Get view name in action attribute of any JSF UI Component using managed bean.
<h:form>
</h:form>
@RequestScoped
//this managed property will read value from request parameter pageId
@ManagedProperty(value = "#{param.pageId}")
if(pageId == null) {
return "home";
if(pageId.equals("1")) {
return "page1";
}else if(pageId.equals("2")) {
return "page2";
}else {
return "home";
<h:form>
</h:commandLink>
</h:commandLink>
<h:commandLink action = "#{navigationController.showPage}" value = "Home">
</h:commandLink>
</h:form>
return "page";
return "page";
<navigation-rule>
<from-view-id>home.xhtml</from-view-id>
<navigation-case>
<from-action>#{navigationController.processPage1}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{navigationController.processPage2}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
</navigation-rule>
JSF will resolve the view name, page1 as view name is page and from-
action in faces-config is navigationController.processPage1
Forward vs Redirect
JSF by default performs a server page forward while navigating to another page and
the URL of the application does not change.
<h3>Forward</h3>
<h3>Redirect</h3>
</h:form>
Here, when Page1 button under Forward is clicked, you will get the following result.
Here when Page1 button under Redirect is clicked, you will get the following result.
Example Application
Let us create a test JSF application to test all of the above navigation examples.
Step Description
Create NavigationController.java under a package com.tutorialspoint.testas
2
explained below.
Create page1.xhtml and page2.xhtml and modify home.xhtml under
5
a webapp folder as explained below.
Compile and run the application to make sure business logic is working as
6
per the requirements.
Finally, build the application in the form of war file and deploy it in Apache
7
Tomcat Webserver.
Launch your web application using appropriate URL as explained below in
8
the last step.
NavigationController.java
package com.tutorialspoint.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@RequestScoped
@ManagedProperty(value = "#{param.pageId}")
return "page1";
return "page2";
}
public String moveToHomePage() {
return "home";
return "page";
return "page";
if(pageId == null) {
return "home";
if(pageId.equals("1")) {
return "page1";
}else if(pageId.equals("2")) {
return "page2";
}else {
return "home";
}
public String getPageId() {
return pageId;
this.pageId = pageId;
faces-config.xml
<faces-config
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_2_0.xsd"
version = "2.0">
<navigation-rule>
<from-view-id>home.xhtml</from-view-id>
<navigation-case>
<from-action>#{navigationController.processPage1}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page1.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{navigationController.processPage2}</from-action>
<from-outcome>page</from-outcome>
<to-view-id>page2.jsf</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xml
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
page1.xhtml
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:h = "http://java.sun.com/jsf/html">
<h:body>
<h2>This is Page1</h2>
<h:form>
</h:form>
</h:body>
</html>
page2.xhtml
xmlns:h = "http://java.sun.com/jsf/html">
<h:body>
<h2>This is Page2</h2>
<h:form>
</h:form>
</h:body>
</html>
home.xhtml
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:f = "http://java.sun.com/jsf/core"
xmlns:h = "http://java.sun.com/jsf/html">
<h:body>
<h2>Implicit Navigation</h2>
<hr />
<h:form>
<h3>Using Managed Bean</h3>
</h:form>
<br/>
<h2>Conditional Navigation</h2>
<hr />
<h:form>
value="Page1">
</h:commandLink>
value="Page2">
</h:commandLink>
value = "Home">
</h:form>
<br/>
<hr />
<h:form>
</h:form>
<br/>
<hr />
<h:form>
<h3>Forward</h3>
<h3>Redirect</h3>
</h:form>
</h:body>
</html>
Once you are ready with all the changes done, let us compile and run the application
as we did in JSF - Create Application chapter. If everything is fine with your
application, this will produce the following result.
The six phases show the order in which JSF processes a form. The list shows the
phases in their likely order of execution with event processing at each phase.
During this phase, JSF builds the view, wires event handlers and validators to UI
components and saves the view in the FacesContext instance. The FacesContext
instance will now contain all the information required to process a request.
If the local value is invalid, JSF adds an error message to the FacesContext instance,
and the life cycle advances to the render response phase and displays the same page
again with the error message.
After the content of the view is rendered, the response state is saved so that
subsequent requests can access it and it is available to the restore view phase.
This chapter also describes how to include custom objects created by application developers and component
writers on a JSP page.
The JavaServer Faces implementation provides FacesServlet, whose instances accept incoming requests
and pass them to the implementation for processing. Therefore, the application does not need to include a
servlet (such as the Dispatcher servlet) that processes request parameters and dispatches to application
logic, as do the other versions of Duke’s Bookstore.
A custom image map component that allows you to select the locale for the application.
Navigation configured in a centralized application configuration resource file. This eliminates the need to
calculate URLs, as other versions of the Duke’s Bookstore application must do.
Backing beans associated with the pages. These beans hold the component data and perform other
processing associated with the components. This processing includes handling the event generated when a
user clicks a button or a hyperlink.
The table that displays the books from the database and the shopping cart are rendered with
the dataTable tag, which is used to dynamically render data in a table. The dataTable tag on tut-
install/javaeetutorial5/examples/web/bookstore6/web/bookshowcart.jsp also includes input
components.
The table that displays the books from the database uses a c:forEach JSTL tag, demonstrating that you can
easily use JavaServer Faces component tags with JSTL tags.
A custom validator and a custom converter are registered on the credit card field of
the bookcashier.jsp page.
A value-change listener is registered on the Name field of bookcashier.jsp. This listener saves the name in
a parameter so that tut-
install/javaeetutorial5/examples/web/bookstore6/web/bookreceipt.jsp can access it.
This version of Duke’s Bookstore includes the same pages listed in Table 5-1. It also includes the tut-
install/javaeetutorial5/examples/web/bookstore6/web/chooselocale.jsp page, which displays the
custom image map that allows you to select the locale of the application. This page is displayed first and advances
directly to the bookstore.jsp page after the locale is selected.
To deploy and run the application using NetBeans IDE, follow these steps:
1. Perform all the operations described in Accessing Databases from Web Applications.
2. In NetBeans 5.5, select File→Open Project.
3. In the Open Project dialog, navigate to:
tut-install/javaeetutorial5/examples/web/
4. Select the bookstore6 folder.
5. Select the Open as Main Project check box and the Open Required Projects check box.
6. Click Open Project.
7. In the Projects tab, right-click the bookstore6 project, and select Undeploy and Deploy.
8. To run the application, open the bookstore URL http://localhost:8080/bookstore6.
To deploy and run the application using Ant, follow these steps:
To learn how to configure the example, refer to the web.xml file, which includes the following elements:
A display-name element that specifies the name that tools use to identify the application.
A context-param element that specifies that the javax.faces.STATE_SAVING_METHOD parameter has a
value of client, meaning that state is saved on the client.
A listener element that identifies the ContextListener class used to create and remove the database
access.
A servlet element that identifies the FacesServlet instance.
A servlet-mapping element that maps FacesServlet to a URL pattern.
Nested inside a jsp-config element is a jsp-property-group element, which sets the properties for the
group of pages included in this version of Duke’s Bookstore. See Setting Properties for Groups of JSP
Pages for more information.
Setting Up a Page
A typical JavaServer Faces page includes the following elements:
A set of tag library declarations that declare the two JavaServer Faces tag libraries
A view tag
A form tag
This section tells you how to add these elements to your pages and briefly describes the subview tag for including
JavaServer Faces pages inside other pages.
To use the JavaServer Faces UI components in your JSP page, you need to give the page access to the two
standard tag libraries: the JavaServer Faces HTML render kit tag library and the JavaServer Faces core tag
library. The JavaServer Faces standard HTML render kit tag library defines tags that represent common HTML
user interface components. The JavaServer Faces core tag library defines tags that perform core actions and are
independent of a particular render kit.
Using these tag libraries is similar to using any other custom tag library. This chapter assumes that you are familiar
with the basics of using custom tags in JSP pages (see Using Custom Tags).
As is the case with any tag library, each JavaServer Faces tag library must have a TLD that describes it.
The html_basic TLD describes the JavaServer Faces standard HTML render kit tag library. The jsf_core TLD
describes the JavaServer Faces core tag library.
To use any of the JavaServer Faces tags, you need to include these taglib directives at the top of each page
containing the tags defined by these tag libraries:
The uri attribute value uniquely identifies the TLD. The prefix attribute value is used to distinguish tags
belonging to the tag library. You can use other prefixes rather than the h or f prefixes. However, you must use the
prefix you have chosen when including the tag in the page. For example, the form tag must be referenced in the
page using the h prefix because the preceding tag library directive uses the h prefix to distinguish the tags defined
in html_basic.tld:
<h:form ...>
A page containing JavaServer Faces tags is represented by a tree of components. At the root of the tree is
the UIViewRoot component. The view tag represents this component on the page. Therefore, all component tags
on the page must be enclosed in the view tag, which is defined in the jsf_core TLD:
<f:view>
... other JavaServer Faces tags, possibly mixed with other
content ...
</f:view>
You can enclose other content, including HTML and other JSP tags, within the view tag, but all JavaServer Faces
tags must be enclosed within the view tag.
:language:[{-,_}:country:[{-,_}:variant]
The form tag is nested inside of the view tag. As its name suggests, the form tag represents a form, which is
submitted when a button or hyperlink on the page is clicked. For the data of other components on the page to be
submitted with the form, the tags representing the components must be nested inside the form tag. See Adding a
Form Component for more details on using the form tag.
If you want to include a page containing JavaServer Faces tags within another JSP page that includes JavaServer
Faces tags, you must enclose the entire nested page in a subview tag. You can add the subview tag on the
parent page and nest a jsp:include inside it to include the page:
<f:subview id="myNestedPage">
<jsp:include page="theNestedPage.jsp" />
</f:subview>
You can also include the subview tag inside the nested page, but it must enclose all the JavaServer Faces tags
on the nested page.
The rendered attribute can be set to true or false, indicating whether or not the components nested in
the subview tag should be rendered.
In summary, a typical JSP page that uses JavaServer Faces tags will look somewhat like this:
<f:view>
<h:form>
other JavaServer Faces tags and core tags,
including one or more button or hyperlink components for
submitting the form
</h:form>
</f:view>
The sections Using the Core Tags and Adding UI Components to a Page Using the HTML Component
Tags describe how to use the core tags from the JavaServer Faces core tag library and the component tags from
the JavaServer Faces standard HTML render kit tag library.
Facet tag facet Signifies a nested component that has a special relationship
to its enclosing tag
Container tag subview Contains all JavaServer Faces tags in a page that is included
in another JSP page containing JavaServer Faces tags
Output tag verbatim Generates a UIOutput component that gets its content from
the body of this tag
Container for view Encloses all JavaServer Faces tags on the page
form tags
These tags are used in conjunction with component tags and are therefore explained in other sections of this
tutorial. Table 11-2 lists the sections that explain how to use specific jsf_core tags.
For these tags, you need to use the following namespaces of URI in html node.
<html
xmlns = "http://www.w3.org/1999/xhtml"
xmlns:f = "http://java.sun.com/jsf/core">
f:convertNumber
1
Converts a String into a Number of desired format
f:convertDateTime
2
Converts a String into a Date of desired format
Custom Convertor
3
Creating a custom convertor
For these tags you need to use the following namespaces of URI in html node.
<html
xmlns = "http://www.w3.org/1999/xhtml"
xmlns:f = "http://java.sun.com/jsf/core">
f:validateLength
1
Validates the length of a string
f:validateLongRange
2
Validates the range of a numeric value
f:validateDoubleRange
3
Validates the range of a float value
f:validateRegex
4
Validates JSF component with a given regular expression
Custom Validator
5
Creates a custom validator