Beginner Problem: One UI per Session

Hello,

I have a question concerning the Scope of a an Vaadin UI object. I assumed that there will be one instance per user session. In my example application however this is not the case. Every time I reload the page in my browser a new UI instance is created and the data of the old one is lost. Is this the normal behavior or am I doing something wrong? The session id does not change if reload the page.

Here is my example:


package master.afb.example;

import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class View extends UI {

    private static final Logger log = LoggerFactory.getLogger(View.class);
    private Label label = new Label();
    private Button button = new Button("clickMe");
    private int counter = 0;

    @Override
    protected void init(VaadinRequest request) {
        log.debug("New View created");
        this.getPage().setTitle("Hello World");
        label.setCaption("Hello World");
        button.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                label.setCaption("You cklicked "+ ++counter+" times");
            }
        });
        this.addComponent(label);
        this.addComponent(button);
    }

    public Label getLabel() {
        return label;
    }

    public Button getButton() {
        return button;
    }
}

web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<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">
    <display-name>Vaadin Web Application</display-name>
    <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
            <description>Vaadin UI to display</description>
            <param-name>UI</param-name> 
            <param-value>master.afb.example.View</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/*</url-pattern> 
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

Matthias, use [b]
[font=Courier New]

[/font]com.vaadin.annotations.PreserveOnRefresh
[/b] annotion with your UI:

@PreserveOnRefresh
public class View extends UI...

See this tutorial:
Preserve On Refresh

Cheers,
Christian

There can be multiple UIs per user session. Multiple tabs or otherwise. But if you want to have a UI to survive a page refresh annotate UI with @PreserveOnRefresh