ViewScoped example in JSF

A small ViewScoped example.

Here is my pom.xml
<project xmlns=""
    <name>jsf-one Maven Webapp</name>

and web.xml
<web-app xmlns=""

Here is my ViewScoped ManagedBean 
package biz.tugay.jsfone;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.util.Random;
 * User: on 10/05/16 at 11:51.
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="">
    <title>Hello World!!!</title>
    <h:outputLabel value="#{myManagedBean.randomNumber}"/>
        <h:commandButton  action="#{myManagedBean.doNothing}" value="Save" />

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!