JSF 2.0 hello world example
In this tutorial, we show you how to develop a JavaServer Faces (JSF) 2.0 hello world example, shows list of JSF 2.0 dependencies, basic annotations and configurations.
Project Environment
This JSF 2.0 example is build with following tools and technologies
- JSF 2.1.7
- Maven 3
- Eclipse 3.6
- JDK 1.6
- Tomcat 6.0.26
First, review the final project structure, in case you are confuse about where should create the corresponding files or folder later.

1. JSF 2.0 Dependencies
Maven central repository has the JSF version up to 1.2 only, to get the JSF 2.0, you may need to download from Java.net repository.
Maven central repository is updated JSF library to 2.1.7. The previous Java.net repository is no longer required.
For Java EE Application Server like Glassfish
In most Java EE application servers, it has build-in support for JSF 2.0, so you need to download single JSF API for development purpose.
... <dependencies> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> <repositories> <repository> <id>java.net.m2</id> <name>java.net m2 repo</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> ...
For simple servlet container like Tomcat
This is a bit troublesome, you need to download following dependencies.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 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.mkyong.common</groupId> <artifactId>JavaServerFaces</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>JavaServerFaces Maven Webapp</name> <url>http://maven.apache.org</url> <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> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>com.sun.el</groupId> <artifactId>el-ri</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <finalName>JavaServerFaces</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>
For more detail about the JSF 2.0 dependencies, please refer to this official JSF 2.0 release note.
The el-ri.jar is an arguable dependency in Tomcat servlet container, even it’s not stated in the release note, but you need this library to solve the “JSP version of the container is older than 2.1…” error message.
This “el-ri.jar” is too old, it’s recommended to use the latest “el-impl-2.2.jar”, from Java.net
<dependency> <groupId>org.glassfish.web</groupId> <artifactId>el-impl</artifactId> <version>2.2</version> </dependency>
2. JSF 2.0 Managed Bean
A Java bean or JSF managed bean, with a name property to store user data. In JSF, managed bean means this Java class or bean can be accessed from a JSF page.
In JSF 2.0, use @ManagedBean annotation to indicate this is a managed bean.
HelloBean.java
package com.mkyong.common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class HelloBean implements Serializable { private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
In JSF 1.x, you had to declare this beans in the faces-config.xml, but this is no longer required in JSF 2.0.
3. JSF 2.0 Pages
In JSF 2.0, it’s recommended to create JSF page in XHTML file format, a file with a .xhtml extension.
See following two JSF 2.0 pages :
To use the JSF 2.0 components or features, just declared the JSF namespace at the top of the page.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
File : hello.xhtml – Renders a JSF text box and link it with the “helloBean” (JSF managed bean), “name” property, and also a button to display the “welcome.xhtml” page when it’s clicked.
<?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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 Hello World</title> </h:head> <h:body> <h3>JSF 2.0 Hello World Example - hello.xhtml</h3> <h:form> <h:inputText value="#{helloBean.name}"></h:inputText> <h:commandButton value="Welcome Me" action="welcome"></h:commandButton> </h:form> </h:body> </html>
In JSF 1.x, you had to declare the “navigation rule” in “faces-config.xml“, to tell which page to display when the button is clicked. In JSF 2.0, you can put the page name directly in the button’s “action” attribute. For simple navigation, it’s more than enough, but, for complex navigation, you are still advise to use the “navigation rule” in “faces-config.xml“.
File : welcome.xhtml – Display the submitted text box value.
<?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>JSF 2.0 Hello World</title> </h:head> <h:body bgcolor="white"> <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3> <h4>Welcome #{helloBean.name}</h4> </h:body> </html>
The #{…} indicate this is a JSF expression language, in this case, #{helloBean.name}, when the page is submitted, JSF will find the “helloBean” and set the submitted textbox value via the setName() method. When welcome.xhtml page is display, JSF will find the same session “helloBean” again and display the name property value via the getName() method.
4. JSF 2.0 Serlvet Configuration
Just like any other standard web frameworks, you are required to configure JSF stuffs in web.xml file.
File : web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <display-name>JavaServerFaces</display-name> <!-- Change to "Production" when you are ready to deploy --> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <!-- Welcome page --> <welcome-file-list> <welcome-file>faces/hello.xhtml</welcome-file> </welcome-file-list> <!-- JSF mapping --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map these files with JSF --> <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>
Define a “javax.faces.webapp.FacesServlet” mapping, and map to those well-known JSF file extension (/faces/*, *.jsf, *.xhtml,*.faces).
In this case, the below 4 URLs are pointing to the same hello.xhtml.
- http://localhost:8080/JavaServerFaces/hello.jsf
- http://localhost:8080/JavaServerFaces/hello.faces
- http://localhost:8080/JavaServerFaces/hello.xhtml
- http://localhost:8080/JavaServerFaces/faces/hello.jsf
In JSF 2.0 development, it’s recommended to set the “javax.faces.PROJECT_STAGE” to “Development“, it will provide many useful debugging information to let you track the bugs easily. For deployment, just change it to “Production“, you just do not want your customer to look at this annoying debugging information :).
5. Demo
A long article end with a project demo :)
URL : http://localhost:8080/JavaServerFaces/hello.jsf

A simple JSF page, with a text box and a button.

When the button is clicked, display the submitted text box value.
Download Source Code
References
- JavaServer Faces Technology
- JSF 2.0 release note
- Wiki : JavaServer Faces
- Wiki : XHTML file explanation
- java.lang.IllegalArgumentException: javax.faces.context.ExceptionHandlerFactory
- JSF 2.0 + Tomcat : It appears the JSP version of the container is older than 2.1…
- Eclipse IDE : Unsupported content type in editor
- Eclipse IDE : .xhtml code assist is not working for JSF tag







[...] JSF 2.0 hello world example A JavaServer Faces (JSF) 2.0 hello world example, shows the JSF 2.0 dependencies, basic annotations and configurations. Let you have a quick idea about how JSF 2.0 look like, and also how it different from JSF 1.x. [...]
tahnk you, it’s clear and simple to undestand
I tried to do my own project following your directions, and even when importing your project I get the same error when starting tomcat :/
Failed to process JAR [jar:file:/C:/Project/mini%20projet/.workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/JavaServerFaces/WEB-INF/lib/jsf-impl-2.1.7.jar!/] for TLD files
java.util.zip.ZipException: invalid LOC header (bad signature)
It seems that 2.1.7 is not compatible with tomcat 7, after having downgraded to 2.1.6 it works perfectly
[...] for EclipseGoogle App Engine Java SDK 1.6.3.1JSF 2.1.7 Note This example is going to reuse this JSF 2.0 hello world example, modify it and merge it with this GAE + Java example.1. New Web Application ProjectIn Eclipse, [...]
[...] JSF 2.0 hello world example – April 24th ( tags: java javaserver faces jsf example hello_world programming code ) [...]
Hi Mkyong,
your explanation was in details and crystal clear.
Thanks for sharing to community.
– srihari konakanchi
Hema:
Very good topic…
Thanks
Thanks… your article it’s very good.
nice tutorial… just learning java….
damn gud… tnx a ton….:)
hi mkyong……your articals are good……………
so nice but u donot provide the faces-config.xml with out this file how can navigate to next page..
JSF2 support implicit navigation – http://www.mkyong.com/jsf2/implicit-navigation-in-jsf-2-0/
I’ve tried this sample to just get a feel of JSF2.0. But I get the following error. I use Tomcat 6. Do you know how to fix this issue?
javax.el.PropertyNotFoundException: /hello.xhtml @14,46 value=”#{helloBean.name}”: Target Unreachable, identifier ‘helloBean’ resolved to null
I found out that if you’re running this with a tomcat-maven-plugin, you need to start your server with tomcat:run-war. Simply using tomcat:run will not be able to resolve the ManagedBean Annotations.
i am not using the tomcat maven plugin, i just start the tomcat server using the catalina start command. i see the same error. i am using tomcat 7. with tomcat 6 i cant even get to the hello page.
Unfortunanely, it doesn’t work in Tomcat7 environment. There are some problems with el-ri. But in Tomcat6 it works!
Hi mkyong,
Thanks for such a simple and straight forward tutorial.
I have a doubt, how can I use view files with a different extension, other than XHTML, still map to the Faces Servlet ?
I am migrating a JSF 1.2 app to 2.0, and I use file name extensions jspx. I dont want to change them to xhtml.
Hi Upon deploying this example I am getting this exception
com.sun.facelets.component.RepeatRenderer cannot be cast to javax.faces.render.Renderer
and the application is not getting deployed
hi, any how to run JSF2 hello world with Netbeans IDE
Netbean is much more easy, just choose a JSF project will do.
Hi mkyong
“to get the JSF 2.0, you may need to download from Java.net repository.”
how can I download the Java.net repository? By clicking on the link, I get only some folders. how can I implement them?
“For simple servlet container like Tomcat
This is a bit troublesome, you may need to download the following dependencies.”
In which file do I have to do this?
Thanks
Olaf
Hi, this project is Maven-based, you can download the project dependency via Maven command.
[...] JSF 2.0 hello world example A JavaServer Faces (JSF) 2.0 hello world example, shows the JSF 2.0 dependencies, basic annotations and configurations. Let you have a quick idea about how JSF 2.0 look like, and also how it different from JSF 1.x. [...]
Very good article and straight to the point. Your notes comparing JSF 1.* with 2.* along the article proved to be very quite helpful indeed.
Thanks a lot.
[...] Saat ini sedang bermain dengan teknologi Java yaitu JSF 2.0. Mudah mengerjakan form dengan cepat karena ada beberapa component yang sudah ada. Kalau Anda masih pemula, bisa belajar JSF 2.0 hello world example. [...]
WHAT about the jar files of jsf,where we need to save those files.i m using eclipse hellios with tomcat 6.0.Plz tell me the proper stucture of JSF how all the files should be kept.
Maven will handle it for you, usually all project dependencies will put inside the “WEB-INF\lib” folder.
Nice, helpful for a lot of people beginning with JSF! Thanks! :)
[...] a normal HTML tag, it’s extremely easy. In this tutorial, you will restructure the last JSF 2.0 hello world example, so that, when the button is clicked, it will make an Ajax request instead of submitting the whole [...]
how to add/install maven in Eclipse 3.6, Helios.
Get a Maven-Eclipse plugin from Google. Personally, i used to integrate Maven command into Eclipse via Eclipse’s “External Tool Configuration” option, which enable Eclipse to execute all the Maven tasks easily :)