Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Form Validation using xml in struts2

 
hemanth acharya
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am trying to develop an app using struts which has form with fields. I am validating the fields using the validation xml, but its not showing any error while some of the fields are empty.. Could anyone help me to find a loophole in my app, I am new to Struts, any help is greatly appreciated...


Following are the codes in my app,

My initial page with form,



My validation xml named LoginAction-Validation.xml,



My action class..




My struts.xml is the following...





Thanks and regards,
hemanth.
 
Ravindra B Pawar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

CORRECT CODE

you have not specified the action class name

OR BETTER WAY IS TO


CHANGE THIS TO


i think you getting the point

hope this helps
 
hemanth acharya
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravindra,

Thanks for the response,

I have done the changes you mentioned, its still not checking the validation xml to get the errors while the fields in the form is empty.


regards,
hemanth.
 
Ravindra B Pawar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with your code was you were displaying the login jsp page (for first time visit to page) for that you created one dummy action login
Then you were submitting the for to same login action which was simply displaying the form again

if you submit form to the new action which will process your form and corresponding xml should be referred for the validations

for more info you can see code at
http://www.vaannila.com/struts-2/struts-2-example/struts-2-validation-using-xml-example-1.html
 
hemanth acharya
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravindra,

I solved the problem...
The changes you mentioned, I had corrected, along with that i added a property file name struts.properties in which i included the constant,


where 'LoginAction' is my property file containing all the variables, eg..




regards,
hemanth.
 
Ravindra B Pawar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hemanth,

I have never used this property files.
Can you please show me how is you directory strutcture so that i can understand where to put
struts.PROPERTIES
LoginAction.PROPERTIES

Thanks,
Ravi
 
hemanth acharya
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravindra,

I created a new source folder for the project called 'resources' and places in that... Please find the attached image.

regards,
hemanth.
directory structure.GIF
Directory structure
[Thumbnail for directory structure.GIF]
 
Ravindra B Pawar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for quick reply.

It really good idea to create new folder called resources to put these things like struts.xml and properties file.

Do you have any idea if we can put the LoginAction-validation.xml in resources folder or it has to be there with the source file LoginAction.java?

Thanks
 
hemanth acharya
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravindra,

Conventional, the 'action' and the 'validation xml' for the same should be in one folder...
I think it wont be found if its placed in 2 different folders..


regards,
hemanth.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic