This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts validation not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts validation not working" Watch "Struts validation not working" New topic
Author

Struts validation not working

Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi All,

I'm new to struts programming and was trying to create a small form which uses the most basic required validation.

When I write this to include validator plugin everything works fine:


But when I write this the validation doesn't happen.


Is there some property or something that I need to set in order to use the local validator-rules.xml file.

Please Help
[ November 01, 2007: Message edited by: Asha Pathik ]

SCJP 1.5
Vinodh Devasenan
Greenhorn

Joined: Oct 13, 2003
Posts: 3
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi All,

Please help me with struts validation.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I am not sure...your not working example looks exactly like what I have (I am using Struts 1.1). Do you see any errors at startup? I suspect that the problem is with an error in your validator-rules.xml file, or that the file does not exist in the specified directory.

- Brent
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52

Is there some property or something that I need to set in order to use the local validator-rules.xml file.


I suspect that the compiled version of your older code is being referenced by the server...

Could you clear your server's temp dir (where it stores the class file and all that...), recompile the files again, restart server and test it?
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Brent,
In Tomcat Server logs, I'm getting no error but there is one warning regarding resource file:


Bhasker,
I tried deleting all files and restarting the server but still I'm not able to do validation using my validator-rules file.

For your reference I'm pasting all the files:
struts-config.xml


AddressJAvascriptValidation.JSP file



Validation.xml


Alll the xmls. are in WEB-INF folder and MessageResource.properties file is in /web-inf/src/java folder.

Please help

[ November 11, 2007: Message edited by: Asha Pathik ]
[ November 11, 2007: Message edited by: Asha Pathik ]
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
You mentioned

MessageResource.properties file is in /web-inf/src/java folder

but in your struts-config.xml, you've specified


I think you must update the struts-config.xml like something below -


Also, please check the filename, is it MessageResource.properties or MessageResources.properties??
[ November 12, 2007: Message edited by: Bhaskar Reddy ]
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
Also, w.r.t. the validator-rules.xml not being used, can you create a folder under WEB-INF (say 'config') and put both validation.xml & validator-rules.xml there?
And then update your struts-config.xml like below -


Please verify if your server contains the appropriate xml file...
[ November 12, 2007: Message edited by: Bhaskar Reddy ]
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi Bhasker,

Finally I'm able to do validation using local validator-rules.xml but there is one more problem.. the error messages are getting printed on the same page instead of appearing a alert message box.

Any idea how to solve this one ...

Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi,

Can someone please copy validator-rules.xml file here? I think the problem is with my xml file because none of changes are getting saved.

Regards
Asha
[ November 13, 2007: Message edited by: Asha Pathik ]
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
In your jsp page you are using -


Struts displays error messages (if any) in the page where html:errors is being used.

the error messages are getting printed on the same page instead of appearing a alert message box.


If you want the error messages to get printed in an alert box, please change your code as provided in the link below -
display error in alert box
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Thanks Bhaskar, everything is working now.
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi Bhaskar,

Again my validation is not working properly. Changes in MessageResources.properties file is displaying message like this.



Plus message in alert box is a concatination of these messages and is displayed in one single line.

Please help.
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
I guess there is an entry missing for 'required' field validation. Please put an entry (key must be the same as that used in your validator-rules.xml for 'required' field validation) as below -


If you are using struts validation framework, please follow the instructions as provided in the framework (you may read the entire steps as outlined here).

Besides, the error object will have a list of all the errors that occurred during the validation process. You should write your own logic to separate out the field specific errors.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts validation not working