Hi..
I posted earlier my suggestion to the action chaining problem...
Well, here's a bare-bones implementation of what I was talking about.
Again, this may be totally f*cked up but it's your job to tell me
what I'm doing wrong :)
I've attached my (partial) implementation to this post..

/tuomo
Page A:
- has the following <app:returnStamp> tag
<app:returnStamp to="pageA.do" from="pageB.do" />

- if there is access to page B from some other page C we should also add the
  tag to page C otherwise there is the possibility that we would return
  to page A accidentally
<app:returnStamp to="pageC.do" from="pageB.do" />

// ----------------------------------------------------------------------------

//
// ActionBase class. Handles all redirects required by ReturnStamp
//
class ActionBase extends Action {
  //
  // ....
  //
  public final ActionForward perform( ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response )
      throws IOException, ServletException {

    // NOTE: we first have to call the doPerform - method so that after
    //       we get the stamp from the session and store it in the request,
    //       it doesn't mess up this actions request processing
    ActionForward forward = doPerform( mapping, form, request, response );

    // Check for possible return stamps
    HttpSession session   = request.getSession();
    ReturnStamps stamps   = (ReturnStamps)session.getAttribute( 
Constants.RETURN_STAMPS_KEY );
    ActionForward stampForward = null;

    if( stamps != null ) {
      ReturnStamp stamp = stamps.findStamp( request.getServletPath());
      if( stamp != null ) {
        stampForward = new ActionForward( stamp.getTo());

        // Remove this stamp from the session and store it in the request
        stamps.removeStamp( stamp );
        session.setAttribute( Constants.RETURN_STAMPS_KEY, stamps );
        request.setAttribute( Constants.RETURN_STAMP_KEY, stamp );
      }
    }
    if( stampForward != null ) return stampForward;

    return forward;
  }
}

// ----------------------------------------------------------------------------

//
// ReturnStampTag implementation
//
import java.util.Enumeration;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import common.Constants;
import common.ReturnStamp;
import common.ReturnStamps;

public final class ReturnStampTag extends TagSupport {
  protected String to   = null;
  protected String from = null;

  public String getTo() {
    return to;
  }

  public void setTo( String to ) {
    this.to = to;
  }

  public String getFrom() {
    return from;
  }

  public void setFrom( String from ) {
    this.from = from;
  }

  public int doStartTag() throws JspException {
    return SKIP_BODY;
  }

  public int doEndTag() throws JspException {
    HttpSession session = pageContext.getSession();

    if( session == null ) {
      return EVAL_PAGE;
    }

    ServletRequest request = pageContext.getRequest();

    ReturnStamps stamps = (ReturnStamps)session.getAttribute( 
Constants.RETURN_STAMPS_KEY );

    if( stamps == null ) {
      stamps = new ReturnStamps();
    }

    ReturnStamp stamp  = new ReturnStamp( getTo(), getFrom());

    for( Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
      String name = (String)e.nextElement();
      if( name != null ) {
        stamp.addAttribute( name, request.getParameter( name ));
      }
    }

    for( Enumeration e = request.getAttributeNames(); e.hasMoreElements(); ) {
      String name = (String)e.nextElement();
      if( name != null ) {
        stamp.addAttribute( name, request.getAttribute( name ));
      }
    }

    stamps.removeStamp( stamp );
    stamps.addStamp( stamp );

    session.setAttribute( Constants.RETURN_STAMPS_KEY, stamps );

    return EVAL_PAGE;
  }
}

// ----------------------------------------------------------------------------

//
// ReturnStamps implementation
//
import java.util.HashMap;
import java.io.Serializable;

public class ReturnStamp implements Serializable {
  protected String  to         = null;
  protected String  from       = null;
  protected HashMap attributes = new HashMap();

  public ReturnStamp() {
  }

  public ReturnStamp( String to, String from ) {
    this.to   = to;
    this.from = from;
  }

  public String getTo() {
    return to;
  }

  public void setTo( String to ) {
    this.to = to;
  }

  public String getFrom() {
    return from;
  }

  public void setFrom( String from ) {
    this.from = from;
  }

  public Object getAttribute( String name ) {
    return attributes.get( name );
  }

  public void addAttribute( String name, Object obj ) {
    attributes.put( name, obj );
  }
}

// ----------------------------------------------------------------------------

//
// ReturnStamps implementation
//
import java.util.HashMap;
import java.io.Serializable;

public class ReturnStamps implements Serializable {
  protected HashMap stamps = new HashMap();

  public void addStamp( ReturnStamp stamp ) {
    stamps.put( stamp.getFrom(), stamp );
  }

  public ReturnStamp findStamp( String path ) {
    return( (ReturnStamp)stamps.get( path ));
  }

  public void removeStamp( String path ) {
    stamps.remove( path );
  }

  public void removeStamp( ReturnStamp stamp ) {
    stamps.remove( stamp.getFrom());
  }

  public String[] findStamps() {
    return( (String[])stamps.keySet().toArray( new String[stamps.size()] ));
  }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to