ViewScoped example in JSF

A small ViewScoped example.

Here is my 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>
 
        <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>
 
    </dependencies>
 
    <build>
        <finalName>jsf-one</finalName>
    </build>
 
</project>

and 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>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>

Here is my ViewScoped ManagedBean 
package biz.tugay.jsfone;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.Random;
 
/**
 * User: koray@tugay.biz on 10/05/16 at 11:51.
 */
 
@ManagedBean
@ViewScoped
public class MyManagedBean implements Serializable {
 
    private String randomNumber = String.valueOf(new Random().nextInt());
 
    public String getRandomNumber() {
        return randomNumber;
    }
 
    public void doNothing() {
 
    }
}

And my index.xhtml
<!DOCTYPE html>
<html xmlns:h="http://xmlns.jcp.org/jsf/html">
<head>
    <title>Hello World!!!</title>
</head>
<h:body>
    <h:outputLabel value="#{myManagedBean.randomNumber}"/>
    <h:form>
        <h:commandButton  action="#{myManagedBean.doNothing}" value="Save" />
    </h:form>
</h:body>
</html>

So, what is so special about this page. When I first load index.xhtml I will see the value: 1417643166. Which is ok, it is just a random number.. Now I will refresh the page and I will get a different random number: -646659341

BUT! If I click on the Save button, I will keep getting the random number from the previous View! This is because,

@ViewScoped will keep living as long as you post back a page to itself!