You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Axel Seinsche <st...@seinsche.net> on 2004/08/20 15:36:38 UTC

errors get lost on redirect

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


Re: errors get lost on redirect

Posted by Axel Seinsche <st...@seinsche.net>.
Hubert Rabago schrieb:

>Well, not really set the input, but you can direct the request to the
>correct one in your java code.
>First off, set validate=false on the action.  
>Then, in your action, call the form's validate() yourself.
>Lastly, if there are validation errors, call saveErrors() to save the
>error messages then forward the request to the correct input form.
>
>Hubert
>
>  
>
Thank you Hubert, sometimes the best solution is so near and you can't 
see it. ;-) This works perfectly and was the fastest way to realize this.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: errors get lost on redirect

Posted by Hubert Rabago <hr...@gmail.com>.
On Fri, 20 Aug 2004 17:19:24 +0200, Axel Seinsche
<st...@seinsche.net> wrote:
> Bill Siggelkow schrieb:
> 
> > I don't understand exactly why you are doing the redirect? Why not
> > just set the input attribute to be the page you are redirecting to ...
> > then Struts will forward to it when validation fails.
> >
> Can I set the input attribute in my Java Code? I thought I have to
> define it in the struts-config.xml. But at design time it's not yet
> defined to which page the application should forward to. Is it possible
> to use mapping.setInput(myForward)? That would be too easy. ;-)
> 
> TIA, Axel
> 

Well, not really set the input, but you can direct the request to the
correct one in your java code.
First off, set validate=false on the action.  
Then, in your action, call the form's validate() yourself.
Lastly, if there are validation errors, call saveErrors() to save the
error messages then forward the request to the correct input form.

Hubert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: errors get lost on redirect

Posted by Axel Seinsche <st...@seinsche.net>.
Bill Siggelkow schrieb:

> I don't understand exactly why you are doing the redirect? Why not 
> just set the input attribute to be the page you are redirecting to ... 
> then Struts will forward to it when validation fails.
>
Can I set the input attribute in my Java Code? I thought I have to 
define it in the struts-config.xml. But at design time it's not yet 
defined to which page the application should forward to. Is it possible 
to use mapping.setInput(myForward)? That would be too easy. ;-)

TIA, Axel

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: errors get lost on redirect

Posted by Bill Siggelkow <bi...@bellsouth.net>.
I don't understand exactly why you are doing the redirect? Why not just 
set the input attribute to be the page you are redirecting to ... then 
Struts will forward to it when validation fails.

Axel Seinsche wrote:
> 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: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Asunto: errors get lost on redirect

Posted by Guillermo Meyer <gm...@fibertel.com.ar>.
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.
 *@author 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" <us...@struts.apache.org>
>From:	"Axel Seinsche" <st...@seinsche.net>
>Subject: errors get lost on redirect
>To:	Struts Users Mailing List <us...@struts.apache.org>
>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: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org


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



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org