Porting Oracle’s “First Cup” Tutorial to Wildfly / JBoss

  JBoss, Tutorial, Wildfly

Creating the business logic and UI components

Now we will create the second submodule called ‘firstcup-war’. According to the Oracle tutorial chapter 3.2.2, this module will contain the business logic and the UI. Read the Oracle text for a better understanding of the concepts. Don’t pay any attention to project setup stuff. This is not going to work in our environment. Similar to what we did for creating our first module ‘dukes-age’, we now create another maven module. Select File -> New -> Other -> Maven -> Maven Module. See the first screen how it is supposed to look like:

New Maven Module firstcup-war

Click on ‘Next’ and this time select the firstcup-war-archetype in the ‘Default Local’ catalog before continuing:

New Maven Module firstcup-war 2

The next screen has to look like this. Hopefully, you know by now which values are required to make it work:

New Maven Module firstcup-war 3

After the project creation finished, we have to change a few things which the archetype did wrong. Again, its the wrong groupId and the redundant version. See how the ‘firstcup-war’ project’s pom.xml has to look like:


<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<artifactId>firstcup</artifactId>
<groupId>com.neuromechanics</groupId>
<version>7.0.5</version>
</parent>

<groupId>com.neuromechanics</groupId>
<artifactId>firstcup-war</artifactId>
<packaging>war</packaging>
<name>${project.artifactId}</name>
<description>The web front-end for the First Cup Tutorial example.</description>

</project>

Now let’s talk about the ‘firstcup.war’ project’s dependencies. In Oracle’s tutorial you get a few hints. Take a look at chapter 4.2 to 4.4. It talks about JPA (Java Persistence API), EJBs (Enterprise Java Beans), JavaServerFaces and Facelets components. We’ll take care of these dependencies in a minute. You must complete the files which Oracle tells you to complete, starting with chapter 4.2. All required files are already there, created by the maven ‘firstcup-war-archetype’.

As soon as you are finished, it’s time to fill in the missing dependencies in the pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<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>

   <parent>
      <groupId>com.neuromechanics</groupId>
      <artifactId>firstcup</artifactId>
      <version>7.0.5</version>
   </parent>

   <groupId>com.neuromechanics</groupId>
   <artifactId>firstcup-war</artifactId>
   <packaging>war</packaging>
   <name>${project.artifactId}</name>

   <dependencies>
      <dependency>
         <groupId>org.jboss.spec.javax.ejb</groupId>
         <artifactId>jboss-ejb-api_3.2_spec</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.hibernate.javax.persistence</groupId>
         <artifactId>hibernate-jpa-2.1-api</artifactId>
      </dependency>
      <dependency>
         <groupId>org.jboss.spec.javax.faces</groupId>
         <artifactId>jboss-jsf-api_2.2_spec</artifactId>
      </dependency>
      <dependency>
         <groupId>javax.enterprise</groupId>
         <artifactId>cdi-api</artifactId>
      </dependency>
      <dependency>
         <groupId>javax.validation</groupId>
         <artifactId>validation-api</artifactId>
      </dependency>
      <dependency>
         <groupId>org.jboss.resteasy</groupId>
         <artifactId>jaxrs-api</artifactId>
      </dependency>
      <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-entitymanager</artifactId>
      </dependency>
      <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>1.4.191</version>
      </dependency>
   </dependencies>
</project>

This should fix everything. Pay special attention to the dependencies because most of them lack a version element. This is due to the Java EE 7 BOM we put into the parent <code>pom.xml</code>. This BOM defines a compatible set of popular Java EE 7 libraries and framework versions. Some dependencies are obviously not covered by the BOM, so I had to include its versions. Don’t forget to Maven Update Project once in a while (right-click on projects root node in Project Explorer -> Maven -> Update Project).

There is one problem left which we inherited from the ‘firstcup-war-archetype’ archetype. The Facelets based UI  <code>greeting.xhtml, response.xhtml</code> takes string constants from a translation ‘bundle’. This bundle is configured in the file /firstcup-war/src/main/webapp/WEB-INF/faces-config.xml.

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" 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-facesconfig_2_2.xsd">

<application>
   <resource-bundle>
      <base-name>org.firstcup.war.web.WebMessages</base-name>
      <var>bundle</var>
   </resource-bundle>
   <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>es</supported-locale>
   </locale-config>
</application>
</faces-config>

For some reason, the files which contain the translations have been put into a folder which is not a resource folder in our project. The fastest solution would be to copy the <code>WebMessages.properties”</code> and <code>WebMessage_es.properties</code> files to the <code>org.firstcup.war.web</code> java package.

LEAVE A COMMENT