Programmatic Validations With Resource Bundle In Struts2

Struts » on Oct 23, 2011 { 9 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


<%@ taglib prefix="s" uri="/struts-tags" %>

<s:form action="verify">

    <s:textfield name="uname" key="enter.user" /><br>
    <s:password name="password" key="enter.pass" /><br>

    <s:submit value="Click" align="center" /> 



<%@ taglib prefix="s" uri="/struts-tags" %>
Hello <s:property value="uname" />


<%@ taglib prefix="s" uri="/struts-tags" %>
<s:actionerror />
error page is this

package java4s;
import com.opensymphony.xwork2.ActionSupport;

public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private String uname,password;

    public String getUname() {
        return uname;

    public void setUname(String uname) {
        this.uname = uname;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;

    public String execute()
        if(uname.equals("java4s") && password.equals("pass"))
            return SUCCESS;

            return ERROR;

    public void validate()
        if(uname.equals("") || uname.length()==0)
        if(password.equals("") || password.length()==0)




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<?xml version="1.0" encoding="UTF-8"?>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="verify" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="input">/index.jsp</result>


enter.user = User name
enter.pass = Password
user.wrong = You must enter the user
pass.wrong = password should not be enter
u.p.wrong = user or Password is wrong

After Execution


Output if enter wrong user/pass


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack 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.


9 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…

  9. Nilesh says:

    Please provide examples for Declarative validations [ xml validations ]
    Using Annotations too.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

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