This is great. Thanks.

On Wed, Mar 5, 2008 at 9:57 AM, Josh Canfield <[EMAIL PROTECTED]> wrote:

> If you can avoid creating the session in the first place I think that
> would be the best thing to do. The google-able portion of my site
> <plug>www.thedailytube.com</plug> only uses the session for the search
> form. To avoid creating the session for the form I created a new
> persistent field strategy that only creates a session if the value
> being stored is non-null. Then, I extended the Tapestry Form component
> to hold onto the validator in a non-persisted field unless there are
> errors to maintain...
>
> Here is some code that you can use if you like:
>
> ****** NonNullSessionPersistentFieldStrategy.java, essentially a dupe
> of the SessionPersistentFieldStrategy with null check logic ****
>
> package com.thedailytube.ui.tapestry.services;
>
> import org.apache.tapestry.internal.services.PersistentFieldChangeImpl;
> import static
> org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
> import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
> import org.apache.tapestry.services.PersistentFieldChange;
> import org.apache.tapestry.services.PersistentFieldStrategy;
> import org.apache.tapestry.services.Request;
> import org.apache.tapestry.services.Session;
>
> import java.util.Collection;
> import java.util.Collections;
> import java.util.List;
>
> /**
>  * Created by IntelliJ IDEA.
>  * User: joshcanfield
>  * Date: Oct 26, 2007
>  */
> public class NonNullSessionPersistentFieldStrategy implements
> PersistentFieldStrategy {
>  /**
>  * Prefix used to identify keys stored in the session.
>  */
>  static final String PREFIX = "nonnull:";
>
>  private final Request _request;
>
>  protected NonNullSessionPersistentFieldStrategy(Request request) {
>  _request = request;
>  }
>
>  public final Collection<PersistentFieldChange>
> gatherFieldChanges(String pageName) {
>  Session session = _request.getSession(false);
>
>  if (session == null) return Collections.emptyList();
>
>  List<PersistentFieldChange> result = newList();
>
>  String fullPrefix = PREFIX + pageName + ":";
>
>  for (String name : session.getAttributeNames(fullPrefix)) {
>  PersistentFieldChange change = buildChange(name, session.getAttribute
> (name));
>
>  result.add(change);
>  }
>
>  return result;
>  }
>
>  private PersistentFieldChange buildChange(String name, Object attribute)
> {
>  // TODO: Regexp is probably too expensive for what we need here.
> Maybe an IOC InternalUtils
>  // method for this purpose?
>
>  String[] chunks = name.split(":");
>
>  // Will be empty string for the root component
>  String componentId = chunks[2];
>  String fieldName = chunks[3];
>
>  return new PersistentFieldChangeImpl(componentId, fieldName, attribute);
>  }
>
>  public final void postChange(String pageName, String componentId,
> String fieldName, Object newValue) {
>  notBlank(pageName, "pageName");
>  notBlank(fieldName, "fieldName");
>  StringBuilder builder = new StringBuilder(PREFIX);
>  builder.append(pageName);
>  builder.append(':');
>
>  if (componentId != null) builder.append(componentId);
>
>  builder.append(':');
>  builder.append(fieldName);
>  // because we don't want to create a session when the object is null
>  Session session = _request.getSession(newValue != null);
>  if (session != null) {
>  session.setAttribute(builder.toString(), newValue);
>  }
>  }
>
> }
>
> ********************
> *** Form.java ****
> ********************
>
> package com.thedailytube.ui.tapestry.components;
>
> import org.apache.tapestry.ValidationTracker;
> import org.apache.tapestry.ValidationTrackerImpl;
> import org.apache.tapestry.annotations.Persist;
>
> /**
>  * Overrides the core [EMAIL PROTECTED]
> org.apache.tapestry.corelib.components.Form} in order to store the
> validation tracker only
>  * when there is something to track.
>  * <p/>
>  * Created by IntelliJ IDEA.
>  * User: joshcanfield
>  * Date: Oct 26, 2007
>  */
> public class Form extends org.apache.tapestry.corelib.components.Form {
>  @Persist("nonnull")
>  private ValidationTracker _tracker;
>
>  private ValidationTracker _nonPersistedTracker;
>
>  public ValidationTracker getDefaultTracker() {
>  if (_nonPersistedTracker == null) {
>  if (_tracker != null) {
>  // _tracker is loaded via injection magic when it's in the session
>  _nonPersistedTracker = _tracker;
>  } else {
>  _nonPersistedTracker = new ValidationTrackerImpl();
>  }
>  }
>  return _nonPersistedTracker;
>  }
>
>  public void setDefaultTracker(ValidationTracker defaultTracker) {
>  _nonPersistedTracker = defaultTracker;
>  }
>
>
>  protected void onAction() {
>  if (_nonPersistedTracker.getHasErrors()) {
>  _tracker = _nonPersistedTracker;
>  } else {
>  _tracker = null;
>  }
>  }
>
> }
>
> Josh
>
>
> On Tue, Mar 4, 2008 at 10:05 PM, Daniel Leffel <[EMAIL PROTECTED]>
> wrote:
> > Hi,
> > We're in the process of developing a webapp using T5. The site leverages
> > session persistence on many pages, including a number of pages important
> for
> > the natural search content. It seems to us that levering a filter to
> strip
> > the jessionid from non-cookie enabled user agents would be an effective
> way
> > to keep jsessionids from popping up in the url. Other than insuring all
> > pages gracefully handle sessions which are effectively stateless (from
> > non-cookied enabled user agents), are there any other concerns we should
> be
> > thinking about?
> >
> > Danny
> >
>
>
>
> --
> --
> TheDailyTube.com. Sign up and get the best new videos on the internet
> delivered fresh to your inbox.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to