Flash Scope with JSF and Custom Validator!

Here is the example!

Here is my directory layout:

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-one</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
    <name>jsf-one Maven Webapp</name>
 
    <url>http://maven.apache.org</url>
 
    <dependencies>
 
        <!-- JSF Dependencies -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.13</version>
        </dependency>
 
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.13</version>
        </dependency>
        <!-- // JSF Dependencies -->
 
        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <!--// Spring Framework -->

        <!-- 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-one</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">
 
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
 
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
 
    <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>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>index.xhtml</welcome-file>
    </welcome-file-list>
 
</web-app>

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_2.xsd"
              version="2.2">
 
    <!-- This integrates JSF with Spring Framework -->
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
 
</faces-config>

applicationContext.xml
<!--suppress SpringBeanAttributesInspection -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 
    <bean id="flashBean" class="biz.tugay.jsfone.FlashBean" scope="request"/>
    <bean id="customValidator" class="biz.tugay.jsfone.CustomValidator" scope="singleton" />
 
</beans>

FlashBean.java
package biz.tugay.jsfone;
 
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
 
/**
 * 2016/05/13
 * koray@tugay.biz
 */
 
public class FlashBean {
    public String getFirstname(){
        return null;
    }
    public void setFirstname(String firstname) {
        final Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        flash.put("firstname", firstname);
    }
}

CustomValidator.java
package biz.tugay.jsfone;
 
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
 
/**
 * 2016/05/13
 * koray@tugay.biz
 */
 
public class CustomValidator implements Validator {
    @Override
    public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) 
throws ValidatorException {
        String s = o.toString();
        if (s != null) {
            s = s.trim();
        }
        if (s == null || s.length() < 5) {
            FacesMessage facesMessage = new FacesMessage("Seems quite short!.", "Make it longer!");
            facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(facesMessage);
        }
    }
}

index.xhtml
<!DOCTYPE html>
<html xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
 
    <h:head>
        <title>A Simple Java Server Faces Example</title>
        <h:outputStylesheet name="css/default.css"/>
    </h:head>
 
    <h:body>
        <div>
            <h:form id="flashform" prependId="false">
                <h:inputText id="firstname" label="firstname" value="#{flashBean.firstname}"
                             requiredMessage="Are you kidding me?" required="true">
                    <f:validateLength binding="#{customValidator}" />
                </h:inputText>
                <br />
                <h:commandButton id="submit" value="Submit" action="pagetwo?faces-redirect=true" />
                <br />
                <br />
                <h:message for="firstname" showSummary="true" showDetail="true" />
            </h:form>
        </div>
    </h:body>
 
</html>

and pagetwo.xml 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
 
<h:head>
    <title>A Simple Java Server Faces Example</title>
    <h:outputStylesheet name="css/default.css"/>
</h:head>
 
<h:body>
    <h:outputText value="#{flash.firstname}" escape="true" />
</h:body>
 
</html>

Ok, let 's give our application a try..