Sometimes when you MUST perform a redirect for some reason (once we have
a problem with multipart forms...) and there are errors in the request scope
you lost them.

We solve this problem as follows:

In the action, where you do saveErrors, just do:
this.saveErrorsInSession(request, errors);

in a base class, add this method:
public void saveErrorsInSession(HttpServletRequest request, ActionErrors
errores) {
        request.getSession().setAttribute(BaseAction.ERROR_KEY, errores);
}

Then for the JSP, we develop a simple custom tag that gets ActionErrors
from session scope and put them in the request scope and remove the errors
from the session:

package ar.com.eds.x71.ui.taglib.misc;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * Obtiene el objeto Error de struts del scope, propiedad property, y lo
pasa al default para struts.
 [EMAIL PROTECTED] Guillermo Meyer
 **/
public class GetErrorsTag extends TagSupport {
  private String scope = "session";
  private String property = null;
  private String remove = "true";

  public void setScope(String c) {
    if (c.equalsIgnoreCase("request")) {
      this.scope = "request";
    } else if (c.equalsIgnoreCase("application")) {
      this.scope = "application";
    } else {
      this.scope = "session";
    }
  }

  public void setProperty(String c) {
    this.property = c;
  }

  public void setRemove(String s) {
    this.remove = (s.equalsIgnoreCase("false")?"false":"true");
  }

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

  public int doEndTag() throws javax.servlet.jsp.JspTagException, JspException
{
    ActionErrors errores = null;

    if (this.property == null) {
      this.property = Action.ERROR_KEY;
    }

    if (this.scope.equals("session")) {
      errores = (ActionErrors) ((HttpServletRequest) 
pageContext.getRequest()).getSession().getAttribute(this.property);
    } else if (this.scope.equals("application")) {
      errores = (ActionErrors) 
pageContext.getServletContext().getAttribute(this.property);
    } else {
      errores = (ActionErrors) ((HttpServletRequest) 
pageContext.getRequest()).getAttribute(this.property);
    }

    if (errores != null) {
      if (this.remove.equals("true")) {
        if (this.scope.equals("session")) {
          ((HttpServletRequest) 
pageContext.getRequest()).getSession().removeAttribute(this.property);
        } else if (this.scope.equals("application")) {
          pageContext.getServletContext().removeAttribute(this.property);
        } else {
          ((HttpServletRequest) 
pageContext.getRequest()).removeAttribute(this.property);
        }
      }
      pageContext.setAttribute(Action.ERROR_KEY, errores, PageContext.REQUEST_SCOPE);
    }

    return EVAL_PAGE;
  }
}

And in the JSP, use this tags:
<x71:getErrors/>
<html:errors/>

Disclaimer: we use Struts 1.0. This of course could be enhaced, for example,
just extending ErrorsTag.


Cheers.
Guillermo.

>-- Mensaje original --
>Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
>From:  "Axel Seinsche" <[EMAIL PROTECTED]>
>Subject: errors get lost on redirect
>To:    Struts Users Mailing List <[EMAIL PROTECTED]>
>Date:  Fri, 20 Aug 2004 15:36:38 +0200 (CEST)
>
>
>Hi all,
>
>in my struts application I have 4 forms which have to be filled to store
>an item in the database. All 4 forms are inherited from one abstract form
>class. As form 3 depends on data from 1 I didn't find a better solution,
>but it works.
>
>The problem is the following. When I submit form 4 I can do the complete
>validation. The ActionErrors are filled correctly, but I return back to
Form
>4 (of course). I then did a <logic:messagesPresent> and inside a redirect
>to page where the error is. The redirect works fine, but the errors get
lost
>on this redirect. Can I somehow preserve my errors when redirecting or
can
>I do this without a redirect. As the ActionClass isn't called I can't call
>a mapping.findForward() or something like this. I tried to put the errors
>object in the request scope before doing the redirect but it's not stored
>- even the session scope didn't work. But that was just a try, I don't
want
>my errors to be in the session scope. I hope someone can enlighten me.
:-)
>
>Regards,
>
>Axel
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]


________________________________________
FiberTel, el nombre de la banda ancha http://www.fibertel.com.ar



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

Reply via email to