NOTE: This is outdated information that applies only to Tapestry 4. For current information see http://tapestry.apache.org/persistent-page-data.html.

Tapestry's client persistence is great, but can be over-zealous in encoding the data to every URL in sight. Sometimes, what you want is for normal navigation links to remain unsullies, but forms to get the client persisted data so that if they cause a page re-render due to validation, everything still works. This persistence strategy does just that...

You'll need a Persistence strategy, a persistence scope, and some hivemodule magic. Once it's done, just use @Persist("client:form") to use.

The strategy:

package com.zillow.web.infrastructure;

import org.apache.tapestry.engine.ServiceEncoding;
import org.apache.tapestry.record.ClientPropertyPersistenceStrategy;

public class FormClientPropertyPersistenceStrategy extends
                ClientPropertyPersistenceStrategy
{
        @Override
    public void addParametersForPersistentProperties(ServiceEncoding encoding, boolean post)
        {
                if ( post )
                        super.addParametersForPersistentProperties( encoding, post );
                
        }
        
}

The scope:

package com.zillow.web.infrastructure;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.engine.ServiceEncoding;
import org.apache.tapestry.record.AbstractPrefixedClientPropertyPersistenceScope;
import org.apache.tapestry.record.PersistentPropertyData;

public class FormClientPropertyPersistenceScope extends
                AbstractPrefixedClientPropertyPersistenceScope
{
    private IRequestCycle _requestCycle;
    
        public FormClientPropertyPersistenceScope()
        {
                super( "form:" );
        }
        
    public boolean shouldEncodeState(ServiceEncoding encoding, String pageName,
            PersistentPropertyData data)
    {
        return pageName.equals(_requestCycle.getPage().getPageName());
    }

    public void setRequestCycle(IRequestCycle requestCycle)
    {
        _requestCycle = requestCycle;
    }
        
}

The hivemodule config:

  <service-point id="FormClientPropertyPersistenceScope" 
                                        interface="org.apache.tapestry.record.ClientPropertyPersistenceScope">
    <invoke-factory>
      <construct class="com.zillow.web.infrastructure.FormClientPropertyPersistenceScope">
        <set-object property="requestCycle" value="infrastructure:requestCycle"/>
      </construct>
    </invoke-factory> 
  </service-point>

  <service-point id="FormClientPropertyPersistenceStrategy" 
                                        interface="org.apache.tapestry.record.PropertyPersistenceStrategy">
    
    <invoke-factory model="threaded">
      <construct class="com.zillow.web.infrastructure.FormClientPropertyPersistenceStrategy">
        <set-object property="request" value="infrastructure:request"/>
        <set-object property="scope" value="service:FormClientPropertyPersistenceScope"/>
      </construct>
    </invoke-factory>
    
  </service-point>

  <contribution configuration-id="tapestry.persist.PersistenceStrategy">
    <strategy name="client:form" object="service:FormClientPropertyPersistenceStrategy"/>
  </contribution>

FormClientPersistence (last edited 2011-01-02 01:55:22 by BobHarner)