The goal of this tutorial is to build a basic Java EE hello world application. We will use eclipse, maven and tomee.

Untitled

Select ‘Create a simple project (skip archetype selection)’ our project is very simple 🙂

2

Fill the project informations, you have to change the packaging from jar to war:

3

The the project has been created, you should have the following folder structure:

Ecl 4

The pom.xml is quite empty …

<modelVersion>4.0.0</modelVersion>
<groupId>ch.javaee</groupId>
<artifactId>helloWorldTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Hello world tutorial</name>

… we have to add the dependency on JSF and add the maven plugin to compile the sources and produce a war file to deploy …

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.javaee</groupId>
<artifactId>helloWorldTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Hello world tutorial</name>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>HelloWorldTutorial</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
</build>

</project>

In the web app folder we have to create a web.xml file

Webxml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<display-name>HelloWorldTutorial</display-name>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<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>
</web-app>

We can now create the controller class

Index

package ch.javaee.helloWorldTutorial.backing;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class IndexBean {

private String world = "world";

public String getWorld(){

return world;

}

}

You can see your new class in the project explorer

Controller

We have now to create the facelet page that shows the hello world message:

Htmlx

<?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"
xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head></h:head>

<h:body>

Hello <h:outputText value="#{indexBean.world}" />

</h:body>

</html>

The code simply writes ‘Hello’ and goes to look for the variable world in the indexBean.

Maven

In Eclipse we can create a maven configuration to execute the goals ‘compile’ et ‘war:war’. This configuration creates the package to be loaded by the application server.

Artifacts

As you can see the application file ‘HelloWorldTutorial.war’ has a size of only 4KB. All the components used by the Java EE application are already in the server library.

We are using TomEE, to deploy the application we can copy the .war file in the web apps directory.

Tomee

The final result:

Result