Another 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 -->
 
    </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">
 
 
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
 
    <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>register.xhtml</welcome-file>
    </welcome-file-list>
 
</web-app>

UserBean.java
package biz.tugay.jsfone;
 
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import java.util.Date;
 
 
/**
 * 2016/05/17
 * koray@tugay.biz
 */
 
@ManagedBean
@SessionScoped
public class UserBean {
 
    protected String firstname;
    protected String lastname;
    protected Date dateOfBirth;
    protected String sex;
    private String email;
    protected String serviceLevel;
 
    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;
    }
 
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
 
    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public String getServiceLevel() {
        return serviceLevel;
    }
 
    public void setServiceLevel(String serviceLevel) {
        this.serviceLevel = serviceLevel;
    }
 
    public void validateEmail(FacesContext context, UIComponent toValidate, Object o) throws ValidatorException {
        String email = (String) o;
        if (!email.contains("@")) {
            final FacesMessage facesMessage = new FacesMessage("Invalid email address");
            throw new ValidatorException(facesMessage);
        }
    }
 
    public String addConfirmedUser() {
 
        final FacesMessage facesMessage;
        String outcome;
 
        if (!firstname.startsWith("k")) {
            facesMessage = new FacesMessage("Try adding a user with username k..");
            outcome = "register";
        } else {
            facesMessage = new FacesMessage("Successfully added new user!");
            outcome = "done";
        }
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        return outcome;
    }
 
}

register.xhtml
<!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:head>
        <title>JSF Registiration Form Application</title>
        <style>
            table, tr {
                border: 1px solid black;
            }
        </style>
    </h:head>
    <h:body>
        <h2>JSF Registiration Application</h2>
        <h4>Registiration Form</h4>
        <h:form prependId="false" id="registirationForm">
            <table class="pure-table">
                <tr>
                    <td>First Name:</td>
                    <td>
                        <h:inputText label="First Name" id="firstname" required="true" value="#{userBean.firstname}"/>
                    </td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>
                        <h:inputText label="Last Name" id="lastname" required="true" value="#{userBean.lastname}"/>
                    </td>
                </tr>
                <tr>
                    <td>Sex:</td>
                    <td>
                        <h:selectOneRadio id="sex" label="Sex" required="true" value="#{userBean.sex}">
                            <f:selectItem itemLabel="Male" itemValue="male"/>
                            <f:selectItem itemLabel="Female" itemValue="female"/>
                        </h:selectOneRadio>
                    </td>
                </tr>
                <tr>
                    <td>Date of Birth:</td>
                    <td><h:inputText id="dateOfBirth" label="Date of Birth" value="#{userBean.dateOfBirth}"
                                     required="true">
                        <f:convertDateTime pattern="dd-mm-yy"/>
                    </h:inputText>(dd-mm-yy)
                    </td>
                </tr>
                <tr>
                    <td>Email Address</td>
                    <td><h:inputText id="email" label="Email Address" required="true"
                                     validator="#{userBean.validateEmail}" value="#{userBean.email}"/></td>
                </tr>
                <tr>
                    <td>
                        Service Level:
                    </td>
                    <td>
                        <h:selectOneMenu id="serviceLevel" label="Service Level" value="#{userBean.serviceLevel}">
                            <f:selectItem itemLabel="Basic" itemValue="basic"/>
                            <f:selectItem itemLabel="Medium" itemValue="medium"/>
                            <f:selectItem itemLabel="Premium" itemValue="premium"/>
                        </h:selectOneMenu>
                    </td>
                </tr>
                <tr>
                    <td>
                        <h:commandButton id="submitRegistirationForm" value="Register" action="confirm"/>
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </h:form>
        <h:messages/>
    </h:body>
</f:view>
</html>

confirm.xhtml
<!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:head>
        <title>JSF Registiration Form Application</title>
        <style>
            table, tr {
                border: 1px solid black;
            }
        </style>
    </h:head>
    <h:body>
        <h:form>
            <h2>JSF Registration App</h2>
            <h4>Registration Confirmation</h4>
            <table>
                <tr>
                    <td>First Name:</td>
                    <td>
                        <h:outputText value="#{userBean.firstname}"/>
                    </td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>
                        <h:outputText value="#{userBean.lastname}"/>
                    </td>
                </tr>
                <tr>
                    <td>Sex:</td>
                    <td>
                        <h:outputText value="#{userBean.sex}"/>
                    </td>
                </tr>
                <tr>
                    <td>Date of Birth:</td>
                    <td>
                        <h:outputText value="#{userBean.dateOfBirth}"/>
                    </td>
                </tr>
                <tr>
                    <td>Email Address:</td>
                    <td>
                        <h:outputText value="#{userBean.email}"/>
                    </td>
                </tr>
                <tr>
                    <td>Service Level:</td>
                    <td>
                        <h:outputText value="#{userBean.serviceLevel}"/>
                    </td>
                </tr>
            </table>
            <p><h:commandButton value="Edit" action="register"/></p>
            <p><h:commandButton value="Confirm" action="#{userBean.addConfirmedUser}"/></p>
        </h:form>
    </h:body>
</f:view>
</html>

done.xhtml
<!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:head>
        <title>JSF Registiration Form Application</title>
        <style>
            table, tr {
                border: 1px solid black;
            }
        </style>
    </h:head>
    <h:body>
        <h:form>
            <h2>JSF Registration App</h2>
            <h4>Registration Confirmation</h4>
            <h:messages />
            <table>
                <tr>
                    <td>First Name:</td>
                    <td>
                        <h:outputText value="#{userBean.firstname}" />
                    </td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>
                        <h:outputText value="#{userBean.lastname}"/>
                    </td>
                </tr>
                <tr>
                    <td>Sex:</td>
                    <td>
                        <h:outputText value="#{userBean.sex}" />
                    </td>
                </tr>
                <tr>
                    <td>Date of Birth:</td>
                    <td>
                        <h:outputText value="#{userBean.dateOfBirth}">
                            <f:convertDateTime pattern="E - (dd/MM/yyyy)"/>
                        </h:outputText>
                    </td>
                </tr>
                <tr>
                    <td>Email Address:</td>
                    <td>
                        <h:outputText value="#{userBean.email}" />
                    </td>
                </tr>
                <tr>
                    <td>Service Level:</td>
                    <td>
                        <h:outputText value="#{userBean.serviceLevel}" />
                    </td>
                </tr>
            </table>
        </h:form>
    </h:body>
</f:view>
</html>