Get all updates of Java4s

Programmatic Validations With Resource Bundle In Struts2

Struts »on Oct 23, 2011 { 8 Comments } By Sivateja

In struts 2 we have 3 types of validations

  • Programmatic validations [ manually ]
  • Declarative validations [ xml validations ]
  • Using Annotations

First am going explain with programmatic validations…

  • If we want to apply manual validations in struts 2, then we need to extend our Action class from ActionSupport
  • We need to override validate() method in our Action class
  • So in our action class we have execute() and validate() 2 methods, among these 2 validate() method will be executed first, because depends on the validations result execute() method will be executed
  • In struts 2 if we get any input validation error then either we can type the error message or we can get the message from a resource bundle
  • If we get any error in validate(), this automatically by default will returns string “input“. We have error in validate() method so execute() will not executes, in struts.xml we will sends login.jsp back will see this in example
  • In login.jsp we no need to write any tag to display the errors from this.addActionError(—-) from action class, automatically errors will displays on the top of the input fields
  • Come to execute(), if there is no errors in validate() controller will comes and executes execute() method, and if we get any errors in execute() we need to add errors to addActionError(–), here we need to add <s:actionerrors/> tag in the input page to display the errors
  • If we want to get the error message from the resource bundle we need to call gettext() method

Complete Example

files required…

  • index.jsp
  • success.jsp
  • error.jsp
  • [ in java4s package ]
  • web.xml [ in web-inf ]
  • struts.xml [ in web-inf/classes folder ]
  • [ in web-inf/classes folder ]
  • [ in web-inf/classes folder ]

Directory Structure






After Execution


Output if enter wrong user/pass

You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


8 Responses to “Programmatic Validations With Resource Bundle In Struts2”
  1. Ibrahim Mahammad says:

    Dear SivaTeja

    Here you explained about Programmatic and Declarative Validations in a very fantastic way. But I didn’t fount example of Annotations validation. Could you please provide that one also.


  2. Ranjith says:

    How can i implement threads in struts 1.x ?

  3. Hemanth says:

    Hi SivaTeja,
    Can you provide validation with separate Bean values. When i am trying it is not doing any validation.


  4. Sreeram says:

    Hi java4s,

    this is good tutorial and to any beginner its helpful but in the example that you explained above please correct the line 5 in index.jsp remove and replace it with since by using even though the validations are failed the page is not displaying any error messages but when replaced with its showing me the errors


  5. street says:

    +1 for If we get any error in validate(), this automatically by default will returns string “input“.

  6. Atul Singh says:

    What should I have to do when I want to display the error message infront of fields. In above example error message displays on the top of the input fields

  7. Manoj soni says:

    There site is very help full…

  8. Vikas Shukla says:

    Hey man… you are doing good work for people like me…Thanks a lot…


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.