3
votes

I'm new to jsf technology and I'm trying to understand when and how the ViewScoped jsf bean is initialized.

I have a sample app with 2 beans

ApplicationScopedBean.java

@ManagedBean
@ApplicationScoped
public class ApplicationScopedBean implements Serializable {
    private int incrementedCounter =0;

    public int getIncrementedCounter() {
        incrementedCounter += 1;
        return incrementedCounter;
    }
}

ViewScopedBean.java

@ManagedBean
@ViewScoped
public class ViewScopedBean implements Serializable {
    @ManagedProperty(value = "#{applicationScopedBean}")
    private ApplicationScopedBean applicationScopedBean;

    private int reincarnationNumber;

    private int accessCounter;

    @PostConstruct
    public void initialize() {
        accessCounter = 0;
        reincarnationNumber = applicationScopedBean.getIncrementedCounter();
        System.out.println("Initializing viewScoped stateManager with reincarnationNumber value = " + String.valueOf(reincarnationNumber));
    }

    public void noAction() {
        //do nothing
    }

    public int getReincarnationNumber() {
        return reincarnationNumber;
    }
    public int getAccessCounter() {
        accessCounter += 1;
        return accessCounter;
    }

    public ApplicationScopedBean getApplicationScopedBean() {
        return applicationScopedBean;
    }
    public void setApplicationScopedBean(ApplicationScopedBean applicationScopedBean) {
        this.applicationScopedBean = applicationScopedBean;
    }
}

ApplicationScoped bean is created only once for the application launch. Every time ViewScoped bean is being created, the reincarnationNumber is being increased by 1.

I also have a simple jsf page to display these values:

index.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>View Scoped bean test</title>
</h:head>

<h:body>
    <h:form>    
        <p>ViewScoped bean reincarnation is <h:outputText value="#{viewScopedBean.reincarnationNumber}"/></p>
        <p>ViewScoped bean access counter is <h:outputText value="#{viewScopedBean.accessCounter}"/></p>

        <h:commandButton type="submit" value="no action" action="#{viewScopedBean.noAction()}"/>
        <h:commandButton type="submit" value="reload this page" action="index"/>
    </h:form>
</h:body>

</html>

The problem:

When I launch the application the first time, I already have reincarnationNumber value equal to 3.

In other words, I have this info displayed in browser:

ViewScoped bean reincarnation is 3
ViewScoped bean access counter is 1

Why is that? Why ViewScoped bean has bean created 3 times?

Thanks in advance!

SOLUTION

As it is stated in the comments, the cause was "start browser" checkbox checked in Run Configurations in my IntelliJ IDEA. The trick is when browser is auto-started from IDE I have viewScopedBean initialized 3 times.

1
Can you describe which environment (jsf version, server type) are you using? Can't recreate the problem with Tomcat 6, Mojarra 2.1.26. I get both of them 1.Xtreme Biker
@XtremeBiker I use Tomcat 7.47 + jsf 2.2.1. jsf library was automatically downloaded by IDE from maven (groupId=org.glassfish; artifactId=javax.faces). As I see, this is glassfish jsf implementation. Maybe it is the cause of the problem?ivstas
@XtremeBiker I started the same project at glassfish 4.0. Now I recieve the value 2 for viewScopedBean.reincarnationNumberivstas
It's perfectly working for me with 2.2.1 (Mojarra, which is glassfish's implementation) + Tomcat 6 (with Tomcat 7 too). BTW, your xml-DOCTYPE declaration is unecessary. Just begin your page with html.Xtreme Biker
@XtremeBiker I've found the source of problem. Actually I had "start browser" checkbox checked in Run Configurations of my IntelliJ IDEA. The trick is when browser is auto-started from IDE I have viewScopedBean initialized 3 times. When I simply manually launch the browser and open the page everything works fine. Thank you!ivstas

1 Answers

0
votes

It's not created 3 times, just called the getter several times... and that's because... you can check this answer:

Why JSF calls getters multiple times