You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Mike Kienenberger <mk...@alaska.net> on 2003/11/11 19:02:19 UTC

Suggestion: way to execute code after form.validate(mapping, request) fails, but before forward to input

I'm using Struts 1.1.

I want to execute code only if validation fails, but before the forward to 
the input action.
Unfortunately, it seems that RequestProcessor.processValidate() has 
tightly-coupled these two activities, leaving no way for such activity to be 
triggered.

At first, I thought I could subclass RequestProcessor and set a boolean 
variable before calling processValidate() and check it on doForward(), 
executing my code if the variable was set, but this will probably fail in a 
multithreaded environment.  Even if I could make this work by temporarily 
setting attributes on my request, it'd still be an ugly hack.

I don't see any reasonable solution to my problem other than subclassing 
RequestProcessor, copying processValidate() into it, and making my change 
there, which seems likely to break after a struts upgrade.

I'd like to recommend that in some future struts version that 
RequestProcessor.processValidate() provide a hook for executing code between 
failed validation and forwarding to the input mapping.

In fact, it seems to me that this whole section of code

======================
        // Has an input form been specified for this mapping?
        String input = mapping.getInput();
        if (input == null) {
            if (log.isTraceEnabled()) {
                log.trace("  Validation failed but no input form 
available");
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                               getInternal().getMessage("noInput",
                                                        mapping.getPath()));
            return (false);
        }

        // Save our error messages and return to the input form if possible
        if (log.isDebugEnabled()) {
            log.debug(" Validation failed, returning to '" + input + "'");
        }
        request.setAttribute(Globals.ERROR_KEY, errors);

        if (moduleConfig.getControllerConfig().getInputForward()) {
            ForwardConfig forward = mapping.findForward(input);
            processForwardConfig( request, response, forward);
        } else {
            internalModuleRelativeForward(input, request, response);
        }
======================

should be moved to the equivalent of a 
RequestProcessor.processValidateFailure() method which could then be 
subclassed.

-Mike

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


Re: Suggestion: way to execute code after form.validate(mapping, request) fails, but before forward to input

Posted by David Graham <gr...@yahoo.com>.
--- Mike Kienenberger <mk...@alaska.net> wrote:
> I'm using Struts 1.1.
> 
> I want to execute code only if validation fails, but before the forward
> to 
> the input action.
> Unfortunately, it seems that RequestProcessor.processValidate() has 
> tightly-coupled these two activities, leaving no way for such activity
> to be 
> triggered.
> 
> At first, I thought I could subclass RequestProcessor and set a boolean 
> variable before calling processValidate() and check it on doForward(), 
> executing my code if the variable was set, but this will probably fail
> in a 
> multithreaded environment.  Even if I could make this work by
> temporarily 
> setting attributes on my request, it'd still be an ugly hack.
> 
> I don't see any reasonable solution to my problem other than subclassing
> 
> RequestProcessor, copying processValidate() into it, and making my
> change 
> there, which seems likely to break after a struts upgrade.
> 
> I'd like to recommend that in some future struts version that 
> RequestProcessor.processValidate() provide a hook for executing code
> between 
> failed validation and forwarding to the input mapping.
> 
> In fact, it seems to me that this whole section of code
> 
> ======================
>         // Has an input form been specified for this mapping?
>         String input = mapping.getInput();
>         if (input == null) {
>             if (log.isTraceEnabled()) {
>                 log.trace("  Validation failed but no input form 
> available");
>             }
>            
> response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
>                                getInternal().getMessage("noInput",
>                                                        
> mapping.getPath()));
>             return (false);
>         }
> 
>         // Save our error messages and return to the input form if
> possible
>         if (log.isDebugEnabled()) {
>             log.debug(" Validation failed, returning to '" + input +
> "'");
>         }
>         request.setAttribute(Globals.ERROR_KEY, errors);
> 
>         if (moduleConfig.getControllerConfig().getInputForward()) {
>             ForwardConfig forward = mapping.findForward(input);
>             processForwardConfig( request, response, forward);
>         } else {
>             internalModuleRelativeForward(input, request, response);
>         }
> ======================
> 
> should be moved to the equivalent of a 
> RequestProcessor.processValidateFailure() method which could then be 
> subclassed.

Sounds like a good candidate for a command in the future Struts Chain
reimplementation of the RequestProcessor.

David

> 
> -Mike
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-dev-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree

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