aspose file tools*
The moose likes Struts and the fly likes Struts 2 execute method after validation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 execute method after validation error" Watch "Struts 2 execute method after validation error" New topic
Author

Struts 2 execute method after validation error

Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
Hi!
I'm using Struts 2 with tiles, and zero configuration setup. I'm performing validation on my actions (with annotations), but I have a problem. I need a series a values of my jsp's, basically lists. So I need to load them before getting to the jsp.

What I do is I have a fetch action that gets called and then this redirects to the jsp that renders the form. So I have my lists in the form. But when I submit the form, if there's a validation error, and I get back to the form, the lists aren't there so my form doesn't work.

I know I cloud use the preparable interface, but I want to avoid that, as I have multiple methods on my action, and the prepare method would have to perform quite a few list fetches on the db, and I want to do that as few times as possible. Ideally I would only have to perform those fetches if the result is "input".

So the solution I've come up with is implementing an interceptor that checks the result, and if it is "input", then execute the method that fetches everything from the db. But that doesn't work. The interceptor works fine, and it does what it's supposed to, and even if I check the valueStack with:

the lists are there, but they don't appear on my jsp.

So, the fetching action is:


This gets me to the jsp with the form. Then the form submits to the action with the validation:

And the action is:


And when this is the result, I get back to the form, but my lists don't get loaded.

Any suggestions? Thanks!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

There is already an interceptor which does this. The prepare interceptor calls the prepare method on action classes in which you can load the list from the database...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
I knew this would happen...
Thanks Ankit Garg, but I just mentioned that at my post.

Emili Calonge wrote:
I know I cloud use the preparable interface, but I want to avoid that.


The prepare interceptor runs the prepare method every time you invoke the action, and I don't want that. When I have my form loaded and I submit, it's a waste of time to execute the prepare method if the action is going to succeed. As you can't know that before executing the action, the prepare interceptor is not what I'm looking for.

As the page you linked states:
http://struts.apache.org/2.0.14/docs/prepare-interceptor.html wrote:
This interceptor is very useful for any situation where you need to ensure some logic runs before the actual execute method runs.


So what I want, is to execute a method only when the validation fails. Well, I think it's quite well explained in my first post.
Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
Well, I finally found a way to do that, but I just don't get why my solution didn't work.

Turns out there's a PreResultListener http://struts.apache.org/2.x/docs/can-we-access-an-actions-result.html that allows me to do that. My interceptor with the PreResultListener is:


Which is very similar to the code I had before, that was:


The InputAware is an interface I've declared which just makes sure that the action has a prepareInput method.

So if I use my implementation I can't access my lists in the jsp, but if I use the befroResult one, everything works just fine. Anyone has an explanation for that? I'd love to understand why mine didn't work so I can fix it, or at least get know better Struts2.

Thanks for your help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts 2 execute method after validation error