May 14, 2021

jetty-maven-plugin, Java 11, JSF 2.3, Primefaces 10 and Java EE 8 with CDI 2.0

Introduction

Using maven jetty plugin is a fast way to develop your web application.

You run your webapp with

$ mvn clean package jetty:run

And whenever you change something the jetty server is automatically reloaded.

pom.xml

<?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>
    <groupId>se.magnuskkarlsson</groupId>
    <artifactId>primefaces-jetty</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <bouncycastle.version>1.65</bouncycastle.version>
        <hibernate.version>5.3.14.Final</hibernate.version>
        <hibernate-validator.version>6.0.18.Final</hibernate-validator.version>
        <resteasy.version>3.6.1.SP2</resteasy.version>
    </properties>

    <dependencies>
        <!-- Java EE 8 -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- JBoss EAP 7.3 -->
        <dependency>
            <groupId>org.eclipse.microprofile</groupId>
            <artifactId>microprofile</artifactId>
            <version>3.0</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>

        <!-- PrimeFaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>10.0.0</version>
        </dependency>
        <!-- https://www.primefaces.org/showcase-ext/views/home.jsf -->
        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>primefaces-extensions</artifactId>
            <version>10.0.0</version>
        </dependency>
        <!-- https://primefaces.github.io/primefaces/10_0_0/#/core/fonticons -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>5.12.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <release>11</release>
                        <showDeprecation>true</showDeprecation>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <!-- https://codenotfound.com/jsf-primefaces-hello-world-example-jetty-maven.html -->
            <!-- mvn clean package jetty:run -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.40.v20210413</version>
                <configuration>
                    <!-- https://www.eclipse.org/jetty/documentation/jetty-9/index.html#jetty-maven-plugin -->
                    <httpConnector>
                        <port>9090</port>
                    </httpConnector>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/primefaces-jetty</contextPath>
                    </webApp>
                </configuration>
                <dependencies>
                    <!-- JSF 2.3 Impl https://javaee.github.io/javaserverfaces-spec/ -->
                    <dependency>
                        <groupId>org.glassfish</groupId>
                        <artifactId>javax.faces</artifactId>
                        <version>2.3.8</version>
                    </dependency>
                    <!-- CDI 2.0 Impl http://fritzthecat-blog.blogspot.com/2019/08/jsf-23-maven-project-in-eclipse.html -->
                    <dependency>
                        <groupId>org.jboss.weld.servlet</groupId>
                        <artifactId>weld-servlet-shaded</artifactId>
                        <version>3.1.2.Final</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Web App

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_4_0.xsd"
    version="4.0">

    <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>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>helloworld.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

src/main/webapp/WEB-INF/faces-config.xml

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

</faces-config>

src/main/webapp/WEB-INF/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_2_0.xsd"
    bean-discovery-mode="all" version="2.0">

</beans>

src/main/webapp/helloworld.xhtml

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>PrimeFaces Hello World Example</title>
</h:head>

<h:body>

    <h:form>
        <p:panel header="PrimeFaces Hello World Example">
            <h:panelGrid columns="2" cellpadding="4">
                <h:outputText value="First Name: " />
                <p:inputText value="#{helloWorld.firstName}" />

                <h:outputText value="Last Name: " />
                <p:inputText value="#{helloWorld.lastName}" />

                <p:commandButton value="Submit" update="greeting" oncomplete="PF('greetingDialog').show()" />
            </h:panelGrid>
        </p:panel>

        <p:dialog header="Greeting" widgetVar="greetingDialog" modal="true" resizable="false">
            <h:panelGrid id="greeting" columns="1" cellpadding="4">
                <h:outputText value="#{helloWorld.showGreeting()}" />
            </h:panelGrid>
        </p:dialog>
    </h:form>

</h:body>
</html>

src/main/java/se/magnuskkarlsson/example/primefaces/HelloWorld.java

package se.magnuskkarlsson.example.primefaces;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class HelloWorld {

    private String firstName = "John";
    private String lastName = "Doe";

    // ----------------------- Logic Methods -----------------------

    public String showGreeting() {
        return "Hello " + firstName + " " + lastName + "!";
    }

    // ----------------------- Helper Methods -----------------------

    // ----------------------- Get and Set Methods -----------------------

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

No comments: