Passing Data Between Pages

Tapestry's policy of doing Post/Redirect/Get can make it a bit tricky for an event request to result in data being passed to the next page.
Here are 5 ways to do it:
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">
<head>
    <style type="text/css">
        .noeg       { padding-bottom: 14px; }
    </style>
</head>
<body class="container">
    <h3>Passing Data Between Pages</h3>
    
    Tapestry's policy of doing <a href="http://en.wikipedia.org/wiki/Post/Redirect/Get">Post/Redirect/Get</a> can make it a 
    bit tricky for an event request to result in data being passed to the next page.<br/>   
    Here are 5 ways to do it:<br/>

    <ul>
        <li>
            <strong>By Activation Context</strong><br/>
            This is the most used technique. It's usually employed to pass an <strong>identifier</strong>, eg. productId.

            <div class="eg">
                <t:form t:id="useactivationcontext">
                    <t:eventlink event="useActivationContext">Use activation context to pass id <em>1</em></t:eventlink>.
                </t:form>
            </div>
        </li>
        
        <li>
            <strong>By Query String</strong><br/>
            This may be the most appropriate way to pass <strong>filter criteria</strong> because, 
            arguably, it produces the most RESTful URL in that situation.

            <div class="eg">
                <t:form t:id="usequerystring">
                    <t:eventlink event="useQueryString">Use query string to pass partial name = <em>humpty</em></t:eventlink>.
                </t:form>
            </div>
        </li>

        <li>
            <strong>By Persist</strong><br/>
            This is usually the most appropriate way to pass <strong>whole objects, lengthy data, or data you don't 
            want to be visible in a URL</strong>

            <div class="eg">
                <t:form t:id="usepersist">
                    <t:eventlink event="usePersist">Use @Persist to pass a Person's first and last name</t:eventlink>.
                </t:form>
            </div>
        </li>

        <li class="noeg">
            <strong>By Session State Objects</strong><br/>
            This is the best way to share data <strong>between multiple pages</strong> as in a <strong>wizard or a shopping basket</strong>.<br/>
            See the <em>Sharing Across Multiple Pages</em> example.
        </li>

        <li>
            <strong>By Services</strong><br/>
            This is the best way to share an object <strong>across the whole application</strong>.<br/>
            See the <em>Sharing Across the Application</em> example.
        </li>
    </ul>   

    <t:pagelink page="Index">Home</t:pagelink><br/><br/>
    
    <t:tabgroup>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/state/PassingDataBetweenPages.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/state/PassingDataBetweenPages.java"/>
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css"/>
    </t:tabgroup>
</body>
</html>


package jumpstart.web.pages.examples.state;

import org.apache.tapestry5.Link;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectPage;

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

    @InjectPage
    private PassingByActivationContext byActivationContextPage;

    @InjectPage
    private PassingByQueryString byQueryStringPage;
    
    @InjectPage
    private PassingByPersist byPersistPage;

    Object onUseActivationContext() {
        byActivationContextPage.set(1L);
        return byActivationContextPage;
    }

    Object onUseQueryString() {
        Link link = byQueryStringPage.set("humpty");
        return link;
    }

    Object onUsePersist() {
        byPersistPage.set("Humpty", "Dumpty");
        return byPersistPage;
    }
}


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