Thorntail can be used to right-size your Java EE applications in a JAR file that can be executed without prior installation of the application server. In this guide, we will learn how to run our Hello World primefaces example HelloWorld Primefaces with Thorntail.

In order to get started, we can generate a basic project which uses CDI and JSF, which are the two required dependencies to run our project. Reach the Thorntail generator at https://thorntail.io/generator/

Thorntail tutorial

Download the generated Maven project.

Adding the Backing Bean:

We will add to our basic project the source code from our original Hello World Primefaces example. Here is the Bean class:

package com.mastertheboss;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.enterprise.inject.Model;

@Model
public class Bean {
    private String word;
 
    private String anagram;
 
    public void generateAnagram() {
        Random rand = new Random();
 
        List<Character> characters = new ArrayList<Character>();
        for (char c : word.toCharArray()) {
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(word.length());
        while (characters.size() != 0) {
            int randPicker = (int) (Math.random() * characters.size());
            output.append(characters.remove(randPicker));
        }
        this.anagram = output.toString();
    }
 
    public String getAnagram() {
        return anagram;
    }
 
    public void setAnagram(String anagram) {
        this.anagram = anagram;
    }
 
    public String getWord() {
        return word;
    }
 
    public void setWord(String word) {
        this.word = word;
    }
}

And the basic index.xhtml page:

<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
    <h:form id="jsfexample">
        <p:panelGrid columns="2">
            <f:facet name="header">Anagram maker</f:facet>
 
            <h:outputLabel for="Word" value="Word" />
            <p:inputText id="Word" value="#{bean.word}" label="Word" />
            <h:outputLabel for="Anagram" value="Anagram" />
            <h:outputText id="Anagram" value="#{bean.anagram}" />
            <f:facet name="footer">
                                
                <p:commandButton action="#{bean.generateAnagram}" update="Anagram"
                    value="Generate anagram" icon="ui-icon-check" style="margin:0" />
            </f:facet>
        </p:panelGrid>
    </h:form>
</h:body>
</html>

Within our pom.xml file, we will just specify to include Primefaces dependency, so that it can be bundler in the final JAR:

<?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/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.example</groupId>
   <artifactId>demojsf</artifactId>
   <name>Thorntail Example</name>
   <version>1.0.0-SNAPSHOT</version>
   <packaging>war</packaging>
   <properties>
      <version.thorntail>2.3.0.Final</version.thorntail>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <failOnMissingWebXml>false</failOnMissingWebXml>
      <primefaces.version>6.0</primefaces.version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>bom-all</artifactId>
            <version>${version.thorntail}</version>
            <scope>import</scope>
            <type>pom</type>
         </dependency>
      </dependencies>
   </dependencyManagement>
   <build>
      <finalName>demo</finalName>
      <plugins>
         <plugin>
            <groupId>io.thorntail</groupId>
            <artifactId>thorntail-maven-plugin</artifactId>
            <version>${version.thorntail}</version>
            <executions>
               <execution>
                  <goals>
                     <goal>package</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
   <dependencies>
      <dependency>
         <groupId>io.thorntail</groupId>
         <artifactId>cdi</artifactId>
      </dependency>
      <dependency>
         <groupId>io.thorntail</groupId>
         <artifactId>jsf</artifactId>
      </dependency>
      <dependency>
         <groupId>org.primefaces</groupId>
         <artifactId>primefaces</artifactId>
         <version>${primefaces.version}</version>
      </dependency>
   </dependencies>
</project>

Testing the Thorntail application

Done with it, we can compile and install our application with:

$ mvn clean install

Now you can test your application, by overriding the default context with "primefaces-demo":

$ java -jar target/demo-thorntail.jar -Dthorntail.context.path=primefaces-demo

As you can see from the following screen, we managed to port our Primefaces application on Thorntail in a snap!

Thorntail tutorial

0
0
0
s2smodern