Table of contents
1.
Introduction
2.
JSF Application
3.
Create a JSF application
3.1.
Create User Interface
3.1.1.
index.xhtml  
3.1.2.
response.xhtml
3.2.
Create a Managed Bean
3.2.1.
User.java
3.3.
Configure Application
3.3.1.
web.xml
3.3.2.
Output
4.
Frequently Asked Question
4.1.
What is the JSF life cycle?
4.2.
What does JSF's managed bean mean?
4.3.
What is Facelet JSF?
4.4.
What does JSF's session scope mean?
4.5.
What is JSF's most recent version?
5.
Conclusion
Last Updated: Mar 27, 2024

JSF Application

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Java Web application framework called JavaServer Faces (JSF) is based on UI components. JSF is a server-based framework, and the JSF UI components' state and prescribed life cycles are represented on the server. The Java EE standard includes JSF.
 

JSF (Java Server Faces) is a framework for designing user interfaces on the server-side by Java

A conventional web container, like Tomcat or Jetty, is where a JSF application runs.

Using JSF's default features solely, this article offers an introduction to JSF.

JSF Application

Web pages containing JSF UI components make up a JSF application. Additionally, a JSF application needs some configuration files ("faces-config.xml" and web.xml).

An pplication is full of interactive UI components

 

  • Managed Bean: A Java class that will be dynamically constructed when the JSF application is running is represented by the data elements of the JSF application (managed beans and backing beans). Which scope the bean is valid for can be specified (Session, Request, Application, or none) switching between web pages
     
  • Ddata validators: Tools for examining the accuracy of user input
     
  • UI and model translation are done using data converters.

 

Managed beans are straightforward Java objects (POJOs) that can be utilized in JSF applications and are declared in "faces-config.xml." You could, for instance, define the Java object "Person." Once the object has been described in faces-config.xml, you can use the Person attributes in your JSF UI components, for example, by binding the object's value "firstName" to a JSF input field.

Create a JSF application

To create a JSF application, we are using NetBeans IDE 8.2. You can also refer to other Java IDEs.

Before we move on to our Java codes, you can brush up your Java skills by watching our Youtube video on Java.

 

Here, we're creating a project, which we'll then launch and test to see how its configuration settings work. So let's start by doing a new project.
 

Step 1: Create a New Project In NetBeans, go to the File menu and select New Project.

 

 

Step 2: Select Category Java Web and Project Web Application.

 

 

Step 3: Enter a project name.

 


Step 4: Select Server and Java EE Version.
 

 

Step 5: Select JSF Framework

 

 

Choose Your Preferred Page Language: JSP is the default language for presentation pages in earlier versions of the JSF framework. Currently, JSF includes the potent tool "Facelets" in versions 2.0 and later. Therefore, we have chosen page language as Facelets here. In the following chapter, we'll go into greater detail on Facelets.

 

 

After you're done, the IDE generates a JSF project for you, complete with a default index.xhtml file. Facelets pages are made using Xhtml, an extension of Html.

 

 

Run: You may now choose the run option after right-clicking the project to start your program. "Hello from Facelets" will be the default message that is produced.

JSF project has been successfully created. The following files are part of this project
 

  • Web.xml is located in the WEB-INF directory
  • index.xhtml is located in the Web Pages directory.

 

The project outputs index.xhtml each time we run it. We will now build an application that consists of a configuration file, two web pages, and a bean class.

To create a new application, the following procedures must be followed:

  • Creating user interface
  • Creating managed beans
  • Configuring and managing FacesServlet

Create User Interface

To render the input web page, we will utilise default page index.xhtml. Make the following changes to your index.xhtml source code:

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://xmlns.jcp.org/jsf/html">  
<h:head>  
<title>User Form</title>  
</h:head>  
<h:body>  
<h:form>  
<h:outputLabel for="username">User Name</h:outputLabel>  
<h:inputText id="username" value="#{user.name}" required="true" requiredMessage="User Name is required" /><br/>  
<h:commandButton id="submit-button" value="Submit" action="response.xhtml"/>  
</h:form>  
</h:body>  
</html>  

 

Create a second web page that produces the output

After creating response.xhtml page. Now, modify it's source code as the given below.

response.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://xmlns.jcp.org/jsf/html">  
<h:head>  
<title>Welcome Page</title>  
</h:head> 
<h:body>  
<h2>Hello, <h:outputText value="#{user.name}"></h:outputText></h2>  
</h:body>  
</html>  

Create a Managed Bean

It is a Java class with getter and setter methods and properties. It serves as a Model in JSF. You can therefore utilize it to create your business logic.

 

After creating a Java class put the below code into your User.java file.

User.java

package managedbean;  
Facelet Title  
import javax.faces.bean.ManagedBean;  
import javax.faces.bean.RequestScoped;  
@ManagedBean  
@RequestScoped  
public class User {  
String name;  
  
	public String getName() {  
   		return name;  
	}  
	public void setName(String name) {  
   		this.name = name;  
	}  
}
You can also try this code with Online Java Compiler
Run Code

Configure Application

A web.xml file in the project is used to set FacesServlet instances and is used to set up the application. Additionally, you can customize your application's welcome page and more.

The web.xml code for this application is shown below.

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">  
<context-param>  
<param-name>javax.faces.PROJECT_STAGE</param-name>  
<param-value>Development</param-value>  
</context-param>  
<servlet>  
<servlet-name>Faces Servlet</servlet-name>  
<servlet-class>javax.faces.webapFacelet Titlep.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>  
<session-config>  
<session-timeout>  
30  
</session-timeout>  
</session-config>  
<welcome-file-list>  
<welcome-file>faces/index.xhtml</welcome-file>  
</welcome-file-list>  
</web-app>  

 

Well! All set. Now run the application.

Output

This is the index page of the application.

When we write CodingNinjas in the input text, this is the response page after submitting the input (index) page.

Frequently Asked Question

What is the JSF life cycle?

When the client sends an HTTP request for a page to the server, the JavaServer Faces application's lifecycle starts, and it ends when the server returns the page. There are two key stages to the JSF lifecycle: The phase of execution Rendering Stage.

What does JSF's managed bean mean?

A normal Java Bean class registered with JSF is the Managed Bean. In other terms, Controlled Beans is a JSF framework managed Java bean. Getter and setter functions, business logic, and even a backing bean are all included in managed beans (a bean contains all the HTML form values).

What is Facelet JSF?

Building component trees and JavaServer Faces views using HTML-style templates requires the usage of Facelets, a robust but lightweight page declaration language.

What does JSF's session scope mean?

You can create and bind objects to a session using the session scope. When this bean is used for the first time in an HTTP request, it is generated, and when the HTTP session is invalidated, it is deleted. Both JSF and CDI provide a request scope that performs similar duties.

What is JSF's most recent version?

JSF 2.3, which was introduced as a part of Java EE 8 in 2017, is the most recent JSF standard as of the time of this writing.

Conclusion

In this article, we have discussed JSF Application.

We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on JSP15 Best Java Frameworks To Use In 2021RubyRuby on RailsJava knowledge for your first coding job 

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Thank you for reading this post :-)

Feel free to upvote and share this article if it has been useful for you.

Live masterclass