Just another tiny JSF example

Here we go..

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>biz.tugay</groupId>
    <artifactId>jsf-two</artifactId>
    <packaging>war</packaging>
 
    <version>0.1-SNAPSHOT</version>
 
    <name>jsf-two Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
 
        <!-- JSF related dependencies -->
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.13</version>
        </dependency>
        <!-- // JSF related dependencies -->
 
        <!-- Already in Tomcat so scope is provided -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!--// Already in Tomcat -->
 
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
 
    <build>
        <finalName>jsf-two</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml
<web-app 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"
         version="3.0">
 
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>page1.xhtml</welcome-file>
    </welcome-file-list>
 
</web-app>

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">
 
    <navigation-rule>
        <from-view-id>/page1.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/page2.xhtml</to-view-id>
            <redirect include-view-params="true">
                <view-param>
                    <name>id</name>
                    <value>#{heritageService.id}</value>
                </view-param>
            </redirect>
        </navigation-case>
    </navigation-rule>
 
</faces-config>

Heritage.java
package biz.tugay.jsfone;
 
/**
 * 2016/05/17
 * koray@tugay.biz
 */
 
public class Heritage {
 
    private long id;
    private String title;
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
}

HeritageService.java
package biz.tugay.jsfone;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
 
/**
 * 2016/05/17
 * koray@tugay.biz
 */
 
@ManagedBean
@RequestScoped
public class HeritageService {
 
    private long id;
    private String title;
 
    public long getId() {
        return id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String addHeritage() {
 
        if (title == null || title.length() == 0) {
            return "failure";
        }
 
        final Heritage heritage = new Heritage();
        heritage.setTitle(title);
 
        id = HeritageInMemoryDB.addNewHeritage(heritage);
 
        return "success";
    }
 
}

HeritageInMemoryDB.java
package biz.tugay.jsfone;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 2016/05/17
 * koray@tugay.biz
 */
 
@ManagedBean
@NoneScoped
public class HeritageInMemoryDB {
 
    private static List<Heritage> heritageInMemoryDB = new ArrayList<Heritage>();
 
    private static long idCounter = -1;
 
    public static long addNewHeritage(final Heritage heritage) {
        idCounter++;
        heritage.setId(idCounter);
        heritageInMemoryDB.add(heritage);
        return idCounter;
    }
 
    public static Heritage getHeritage(Heritage heritage) {
        for (Heritage h : heritageInMemoryDB) {
            if (heritage.equals(h)) {
                return h;
            }
        }
        return null;
    }
 
    public static Heritage getHeritageById(long id) {
        if (id > heritageInMemoryDB.size()) {
            return null;
        }
        return heritageInMemoryDB.get((int) id);
    }
 
    public List<Heritage> getAllHeritages(){
        return heritageInMemoryDB;
    }
 
}

page1.xml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
<f:view>
    <h:body>
        <div>
            <h1>Please add new Heritage</h1>
            <h:form id="addHeritageForm"
                    prependId="false">
                <h:inputText id="heritageTitle"
                             value="#{heritageService.title}"
                             required="true"
                             requiredMessage="Title can not be empty!"/>
                <br/>
                <br/>
                <h:commandButton value="Add new Heritage"
                                 action="#{heritageService.addHeritage()}"/>
                <br/>
                <h:message for="heritageTitle"/>
            </h:form>
        </div>
        <br/>
        <h2>
            <h:outputLabel value="List of all heritages we have"
                           rendered="#{heritageInMemoryDB.allHeritages.size() ne 0}"/>
        </h2>
        <c:forEach items="#{heritageInMemoryDB.allHeritages}"
                   var="heritage">
            #{heritage.id} - #{heritage.title}
            <br/>
        </c:forEach>
    </h:body>
</f:view>
</html>

page2.xml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:view>
    <h:body>
        <div>
            Thank you! You have added a very nice heritage!
            <br />
            #{heritageInMemoryDB.getHeritageById(param.id).id} - #{heritageInMemoryDB.getHeritageById(param.id).title}
            <br />
            <h:outputLink value="page1.xhtml">Go back to the main page!</h:outputLink>
        </div>
    </h:body>
</f:view>
</html>