struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Rank" <>
Subject How to execute code in an Action when ActionForm validation fails?
Date Tue, 18 Apr 2006 17:39:13 GMT
I have a problem, which I am certain you all face regularly:

When validation fails for an ActionForm, I need to set attributes in
the request object, but I can't because the 'execute' code doesn't
run. How have you managed to set attributes on the request object when
validation fails?

I have a couple of solutions in mind.

1. Validating Manually:

>From reading a bit on Rick Reumann's site 
a simple way to handle this is to call the validate method within the
execute method of the Action. There's a bit of logic that has to be
processed in order to capture errors, but it works pretty well.
However, I personally feel like it could lead to some sloppy "execute"

2. Extending the RequestProcessor:

I thought that this idea was genius. And it _almost_ works. My approach was to:
1. Write an interface called "Validatable" with a single method called
"validateFailed(mapping, form, request, response)".
2. Make the Action implement the interface. The code in
"validateFailed" would run even if the validation of the form failed.
Values can be set on the request at this point.
3. Extend the RequestProcessor class, overriding the 'processValidate'
method to look like this:

protected boolean processValidate(
	HttpServletRequest request,
	HttpServletResponse response,
	ActionForm form,
	ActionMapping mapping)
	throws IOException, ServletException, InvalidCancelException
	boolean validated = false;
    	validated = super.processValidate(request, response, form, mapping);
    		Action action = processActionCreate(request, response, mapping);
    		if(action instanceof Validatable){
    		   try {
    			((Validatable) action).validateFailed(mapping, form, request, response);
    	           } catch (Exception e) {}
    	return validated;

4. The code in the 'validateFailed' method runs when the form
vaidation fails. Since the request is sent in as an arg to the
'validateFailed' method, I still have a chance to do some work with

A simple solution! Or so I thought!. The 'validateFailed' code
definitely runs as expected. It sets attributes correctly too. I've
even debugged to see that the request attributes even make it past the
ActionServlet doPost / doGet service methods. But by the time the
request makes it to the jsp, the request attributes are mysteriously
gone. (I'd love to know why!) I'd like to use this approach, because
it seems to fold in the functionality that I want quite elegantly. But
obviously, the fact that the request object can't retain values is a
major setback...

So I'm found wanting. Anything that makes sense and works reliably
will do. Ultimately, I'm looking for a best-practices sort of solution
to this basic problem. What do you do to work around this?

Thanks for your collective wisdom!

Eric Rank

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message