This article was last updated on : November 4th, 2016 at 10:10 am

Easy steps to create JSF web application using Maven build tool

This is a step by step guide to create JSF web application using Maven and Eclipse ide. In the end you should be able to create JSF web application using Maven build tool and Eclipse.

Let us read how to create JSF projects using Maven build tool. Please drop your comment if you faced any problem while executing this example.

Steps to create JSF web application using Maven

  1. We will install required environments to do this example.
  2. We create JSF web application using Maven archetype profile.
  3. Add JSF dependencies to pom.xml file.
  4. Complete pom.xml file.
  5. Configure JSF capabilities in web.xml file.
  6. Create a managed bean.
  7. Create JSP pages.
  8. Create .war file using Maven build tool.
  9. Deploy the created .war file in Apache Tomcat server.
  10. Start Tomcat server and test the web application.

Once you have completed this example you can execute some more JSF examples. We have bunch of tutorials and examples on JSF for you. Each example is providing in depth focus on basic setup, required files, important files – faces-config.xml and web.xml, deploying web application to Apache Tomcat server and testing.

Installations required to create JSF web application

To complete this article you need to install below mentioned environments in your windows 7.

  1. JDK 1.7
  2. Apache Maven 3.3.3
  3. Apache Tomcat 6.20
Go ahead even if you have other versions installed.

This article is written by using above mentioned environments. In case you have configured some different versions of above mentioned environments, I would suggest you to go ahead and do this example. In case you are facing any compilation error or runtime exceptions please drop a comment or contact us. You need to provide details like 1. Your exact error log. 2. System configuration details – Java version, Maven version, JSF version, Apache Tomcat server version. After getting these information from you we can track your issue from here.

In case you have not installed Maven in your system. Please read our article guiding how to install maven in your windows environment.

Create a simple web application using Maven archtype template

We will create a web application in D:\jsf directory. Open command prompt and go to D:\jsf.

Type this command in your command prompt to create JSF web application.

D:\jsf > mvn archetype:generate -DgroupId=com.sitenol.jsf -DartifactId=jsfUsingMaven -DarchetypeArtifactId=maven-archetype-webapp

Press Enter key when command prompt stops at

version: 1.0-SNAPSHOT

Enter ‘y’ when command prompt asks you about package name confirmation

package: com.sitenol.jsf

Wait for the complete execution of the above Maven command. You will see this generated log.

D:\jsf>mvn archetype:generate -DgroupId=com.sitenol.jsf -DartifactId=jsfUsingMaven -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Using property: groupId = com.sitenol.jsf
[INFO] Using property: artifactId = jsfUsingMaven
Define value for property 'version': 1.0-SNAPSHOT: :
[INFO] Using property: package = com.sitenol.jsf
Confirm properties configuration:
groupId: com.sitenol.jsf
artifactId: jsfUsingMaven
version: 1.0-SNAPSHOT
package: com.sitenol.jsf
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.sitenol.jsf
[INFO] Parameter: packageName, Value: com.sitenol.jsf
[INFO] Parameter: package, Value: com.sitenol.jsf
[INFO] Parameter: artifactId, Value: jsfUsingMaven
[INFO] Parameter: basedir, Value: D:\jsf
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\jsf\jsfUsingMaven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:34 min
[INFO] Finished at: 2015-09-06T01:59:51+05:30
[INFO] Final Memory: 16M/219M
[INFO] ------------------------------------------------------------------------

Here we are using maven-archetype-webapp plugin to create web application. We can use maven archetype template to create several types of Java projects. It ranges from a standard simple Java into very complicated Enterprise application. Basically the archetype in maven is a template toolkit that used for creating a standard Java projects.

You can learn more about archetype by visiting Apache official site : Guide to use Maven archetype

Once you have created web application without error using maven. We will find a directory structure as given below.

Image create JSF web application

Add JSF dependencies to pom.xml file

Add JSF dependencies in your pom.xml file inside  <dependencies></dependencies> tags.

<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>

Complete pom.xml

Once you have added the complete JSF dependencies. The pom.xml file will look like as :

<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.sitenol.jsf</groupId>
<artifactId>jsfUsingMaven</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jsfUsingMaven Maven Webapp</name>
<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>jsfUsingMaven</finalName>
</build>
</project>

Configure JSF capabilities in web.xml file.

Now it is time to configure JSF capabilities into web.xml file.

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
Why are we using 4 different types are url pattern in web.xml?

You may be wondering to see 4 url patterns in web.xml. 1. /faces/* 2. *.jsf 3. *.faces 4. *.xhtml

You can use any one pattern. It simply means that you can get same page with multiple urls.
For example these 4 urls will take you to the same page.


1. http://localhost:8080/jsfUsingMaven/faces/welcomeJSF.xhtml
2. http://localhost:8080/jsfUsingMaven/welcomeJSF.jsf
3. http://localhost:8080/jsfUsingMaven/welcomeJSF.faces
4. http://localhost:8080/jsfUsingMaven/welcomeJSF.xhtml

Please try this after deploying your .war file to Apache Tomcat.

Create a managed bean – WelcomeJSFManagedBean.java

Now create a managed bean inside D:\jsf\jsfUsingMaven\src\main\java folder. Since “java” folder is not available you need to create “java” folder.

package com.sitenol.jsf;
import javax.faces.bean.ManagedBean;
@ManagedBean(name = "welcomeJSFMBean", eager = true)
public class WelcomeJSFManagedBean {
public WelcomeJSFManagedBean() {
System.out.println("JSF and Maven example!");
}
public String getMessage() {
return "Welcome to JSF example using Maven";
}
}

Create a JSP page – welcomeJSF.xhtml

<!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">
<head>
<title>JSF Maven Tutorial!</title>
</head>
<body>
#{welcomeJSFMBean.message}
</body>
</html>

Create .war file using Maven build tool.

Now we are all set to create a .war file for the web application. Open command prompt, Go to D:\jsf\jsfUsingMaven directory and type this command to create a .war file.

mvn clean install

Once this command executed successfully you will find the .war file under “target” folder. Now you need to deploy this .war file to Apache Tomcat server.

You will find .war file in target folder. Take the .war file and deploy it in webapp folder of Apache Tomcat Web server.

Now this is the time to start Tomcat server and test the web application. Please enter “http://localhost:8080/jsfUsingMaven/welcomeJSF.xhtml” in your browser and press Enter key.

You should be able to see output for your create JSF web application.

create JSF web application

Result of hello world JSF web application

Please drop a comment if :

  1. You received any problem during reading or executing this example.
  2. You need some more information.
  3. You need any article related to Maven or JSF.

So now you should be able to create JSF web application using Maven build tool and Eclipse.

Thanks for reading this article !

Share this post:

Recent Posts

One Comment

  1. Avatar for Tarun Singh
    Wolex says:

    Many thanks for this simple but clear step-by-step tutorial. I trust this site will greatly help me master JSF.

Leave a Comment