JSF Maven Hello World

JSF Maven Hello World

To start with JSF framework it’s important to understand how to set up simple project which will print some value on web page then slowly move on other topic. In this demo you will see how to set JSF project with one web page where we will invoke managed bean on server side to retrieve data and print its out put on web page.

Tools needed:

  • Eclipse Luna (You can use any version) and install JBoss Tools plug-in into it so that you can create maven project directly from Eclipse.
  • Tomcat server (I will use version 7)

Steps:

  • Create maven project name: JSFMavenHelloWorld in eclipse if you are not sure how to create it please follow this tutorial. Below is final project structure:

JSF Maven Hello World

  • 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.javahonk</groupId>
	<artifactId>JSFMavenHelloWorld</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>JSFMavenHelloWorld 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>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-api</artifactId>
			<version>2.2.11</version>
		</dependency>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-impl</artifactId>
			<version>2.2.11</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>JSFMavenHelloWorld</finalName>
	</build>
</project>
  • 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>Archetype Created Web Application</display-name>
	<welcome-file-list>
		<welcome-file>JSFHelloWorld.xhtml</welcome-file>
	</welcome-file-list>
	
	<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>*.xhtml</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>*.jsf</url-pattern>
	</servlet-mapping>	
</web-app>
  • JSFHelloWorld.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 Hello World!</title>
</head>
<body>
	<h2>JSF Maven Hello World example</h2>
	<h3> Message: #{jSFHelloWorld.message}</h3>
</body>
</html>
  • Managed bean JSFHelloWorld.java:
package com.javahonk;

import javax.faces.bean.ManagedBean;

@ManagedBean(name = "jSFHelloWorld", eager = true)
public class JSFHelloWorld {

	private String message;

	public JSFHelloWorld() {
		message = "Message from bean class";
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

JSF Maven Hello World

JSF Maven Hello World

  • For more information visit JSF documentation here

download  Download Project:  JSFMavenHelloWorld

Leave a Reply

Your email address will not be published. Required fields are marked *