Sharing Across Multiple Pages (1)

This page demonstrates how to share a bit of data between multiple pages using a Session State Object (an SSO).
SSOs are shared between the pages of your web session only, and not with other sessions using the same application.

To turn a field into an SSO, annotate it with @SessionState. If any other page or component in your web session declares a field of the same type and annotates it with @SessionState then it will share the same value.

We use the example of a simple ShoppingBasket object, created in this page and displayed in the next page.
Tapestry creates this ShoppingBasket object the first time the page uses it. Other sessions will not see it, but any pages in your session will use it if they declare a field of type ShoppingBasket annotated with @SessionState.

Caution: A consideration is the impact in clustered servers. Howard discusses this in Tapestry App Replication and the documentation discusses it in Clustering Issues.

References: Session Storage.

Home


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- We need a doctype to allow us to use special characters like &nbsp; 
     We use a "strict" DTD to make IE follow the alignment rules. -->
     
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">
<body class="container">
    <h3>Sharing Across Multiple Pages (1)</h3>

    This page demonstrates how to share a bit of data between multiple pages using a Session State Object (an SSO).<br/>
    SSOs are shared between the pages of your web session only, and not with other sessions using the same application.<br/><br/>
    
    To turn a field into an SSO, annotate it with <code>@SessionState</code>.  If any other page or component in your web session 
    declares a field of the same type and annotates it with <code>@SessionState</code> then it will share the same value.<br/><br/>
    
    We use the example of a simple ShoppingBasket object, created in this page and displayed in the next page.

    <div class="eg">
        <t:form class="form-horizontal">
            <div class="form-group">
                <t:label for="applesQuantity" class="col-sm-2"/>
                <div class="col-sm-2">
                    <t:textfield t:id="applesQuantity" value="myBasket.applesQuantity"/>
                </div>
            </div>
            <div class="form-group">
                <t:label for="orangesQuantity" class="col-sm-2"/>
                <div class="col-sm-2">
                    <t:textfield t:id="orangesQuantity" value="myBasket.orangesQuantity"/>
                </div>
            </div>
            <div class="form-group">
                <t:label for="bananasQuantity" class="col-sm-2"/>
                <div class="col-sm-2">
                    <t:textfield t:id="bananasQuantity" value="myBasket.bananasQuantity"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-2">
                    <t:submit value="Next &gt;" />
                </div>
            </div>
        </t:form>
    </div>
    
    Tapestry creates this ShoppingBasket object the first time the page uses it.  Other sessions will not see it, 
    but any pages in your session will use it if they declare a field of type ShoppingBasket annotated with 
    @SessionState.<br/><br/>

    <strong>Caution:</strong> A consideration is the impact in clustered servers.  Howard discusses this in 
    <a href="http://thread.gmane.org/gmane.comp.java.tapestry.user/65410/focus=65426">Tapestry App Replication</a> 
    and the documentation discusses it in 
    <a href="http://tapestry.apache.org/persistent-page-data.html#PersistentPageData-ClusteringIssues">Clustering Issues</a>.<br/><br/>
    
    References: 
    <a href="http://tapestry.apache.org/session-storage.html">Session Storage</a>.<br/><br/>
    
    <t:eventlink event="GoHome">Home</t:eventlink><br/><br/>
    
    <t:tabgroup>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/state/SharingAcrossMultiplePages1.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/state/SharingAcrossMultiplePages1.java"/>
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/state/examples/state/ShoppingBasket.java"/>
    </t:tabgroup>
</body>
</html>


package jumpstart.web.pages.examples.state;

import jumpstart.web.pages.Index;
import jumpstart.web.state.examples.state.ShoppingBasket;

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

@Import(stylesheet="css/examples/plain.css")
public class SharingAcrossMultiplePages1 {

    // Screen fields
    
    @SessionState
    @Property
    private ShoppingBasket myBasket;
    
    // The code

    Object onSuccess() {
        return SharingAcrossMultiplePages2.class;
    }

    Object onGoHome() {
        // Delete the SSO from the session
        myBasket = null;
        return Index.class;
    }
}


.eg {
                margin: 20px 0;
                padding: 14px;
                border: 1px solid #ddd;
                border-radius: 6px;
                -webkit-border-radius: 6px;
                -mox-border-radius: 6px;
}


package jumpstart.web.state.examples.state;

public class ShoppingBasket {
    private int applesQuantity;
    private int orangesQuantity;
    private int bananasQuantity;
    
    public int getApplesQuantity() {
        return applesQuantity;
    }

    public void setApplesQuantity(int applesQuantity) {
        this.applesQuantity = applesQuantity;
    }

    public int getOrangesQuantity() {
        return orangesQuantity;
    }

    public void setOrangesQuantity(int orangesQuantity) {
        this.orangesQuantity = orangesQuantity;
    }

    public int getBananasQuantity() {
        return bananasQuantity;
    }

    public void setBananasQuantity(int bananasQuantity) {
        this.bananasQuantity = bananasQuantity;
    }
}