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.
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:
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.
Joined: May 17, 2003
Well, I finally found a way to do that, but I just don't get why my solution didn't work.
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.