File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes validate() for multiple actions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validate() for multiple actions" Watch "validate() for multiple actions" New topic

validate() for multiple actions

Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
I've recently started looking into Struts2 and am learning it by building a simple CRUD web application. Compared to Struts1.x, I like the improvements in Struts 2.x, where I can have the same Action class for all operations for my entity. What I mean is, Struts 1.x forced me to have one Action class for each of the CRUD operations of my entity - since execute() method was the only method in the action class, that'd be, well, executed. With Struts2, I can create just one Action class, and implement different methods, one for each of the CRUD operations. Based on how I map them in my struts.xml, the framework will call the specific method for the action I'm invoking. This, coupled with, wildcard action mapping allows to be very concise in struts.xml.

But, when I read about the validate() method in the Action class, I couldn't find a way to have a custom validate() method for each of the action. Needless to say, the inputs for each of the actions is going to be different, and thus the validation also will be different. But, I have only one validate() method. Does this mean that I have to do an if/else on the action name in the validate() method?
I've searched Struts FAQs and this forum but couldn't find any relevant information. I'd appreciate it someone can shed some light on this.
Karthik Shiraly

Joined: Apr 04, 2009
Posts: 873

I don't have an answer regarding validation, but would just like to point out that even in Struts 1.x, it was possible to have single Action with method per operation, using DispatchAction
I agree. Here's the link:
subject: validate() for multiple actions
It's not a secret anymore!