I have written a basic example for struts 2 with validation framework
JSP Page:- having a single field named 'name' and submit button
Action class :- com.package.HelloAction
struts.xml : the action tag has a result with type 'input' and 'success'
HelloAction-validation.xml :- having a requiredstring validation on the name field.
The question is:-
1) why i have to extend ActionSupport to make it working..Isn't that a question against POJO based programming?
2) why is HelloAction-validation.xml needs to be placed under com.package to make it working
Should it not be alnogside classes folder??? the whole concept of configuration files under src package is really confusing??
Rishi Chopra wrote:
1) why i have to extend ActionSupport to make it working..Isn't that a question against POJO based programming?
You don't. ActionSupport simply provides commonly used defaults.
Rishi Chopra wrote:
2) why is HelloAction-validation.xml needs to be placed under com.package to make it working
Should it not be alnogside classes folder??? the whole concept of configuration files under src package is really confusing??
My wild guess is that it is done that way to closely coordinate an action with its validation. It also utilizes the Java class structure to give a namespace to the XML files. If they didn't do it that way, you'd end up with a very crowded root class directory.
1. Without extending ActionSupport it does not work or am i missing something?
2. But if you build your war using maven (mvn clean package) it places the HelloAction-validation.xml away from the .class files
It places it alongside classes folder. ( May be maven war plugin can be configured to do it as well) .
Rishi Chopra wrote:Thanks Joe
1. Without extending ActionSupport it does not work or am i missing something?
It should work without extending ActionSupport. Note that ActionSupport implements several interfaces. If your functionality depends on those interfaces, you'll have to duplicate that implementation or your action will not work.
Rishi Chopra wrote:
2. But if you build your war using maven (mvn clean package)
.
Sorry, I use Ant to do my builds. I can't speak to Maven.
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12612
posted
0
Rishi Chopra wrote:1. Without extending ActionSupport it does not work or am i missing something?
No, you're not missing something. If an action doesn't implement Validateable it won't be validated.
2. But if you build your war using maven (mvn clean package) it places the HelloAction-validation.xml away from the .class files
It places it alongside classes folder. ( May be maven war plugin can be configured to do it as well) .
Correct, but I'm not sure what the question is. Maven's directory structure puts resource files in src/main/resources, and under normal package hierarchy below that. You can put the *source* wherever you want, but it needs to be deployed on the classpath. Maven does this automatically when creating the webapp.
You could just as easily put all your messages in a package.properties at the root of the source tree (which is where messages used across the application should go), or define a global resources file. There are a variety of places Struts 2 will look for messages; see http://struts.apache.org/2.x/docs/localization.html. Wherever you put things, it still needs to be on the classpath.