Struts2 XML Validations Example, Declarative Validations In Struts

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

In struts 2 we have 3 types of validations

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

i already explained how we can do programmatic validations,  now will see how to do declarative validations, friends observe am going to use multiple validations rules for the field mail


files required…

  • index.jsp
  • success.jsp
  • error.jsp
  • [ in java4s package ]
  • LogingEx-validation.xml [ in java4s package, must be with that .class file]
  • web.xml [ in web-inf ]
  • struts.xml [ in web-inf/classes folder ]

Directory Structure


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   <%@taglib uri="/struts-tags" prefix="s" %>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
        <s:head />


<s:actionerror />
<s:form action="verify">

    <s:textfield name="uname" label="Enter Username" required="true"/><br>
    <s:textfield name="age" label="Enter Age" required="true"/><br>
    <s:textfield name="mail" label="Enter Email id" required="true"/><br>

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



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

Hello <s:property value="uname" /><br>
Your Age is:<s:property value="age" /><br>
Your mail:<s:property value="mail" />


This is error page

package java4s;
import com.opensymphony.xwork2.ActionSupport;
public class LogingEx extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private String uname,mail;
    private int age;

    public String getUname() {
        return uname;

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

    public String getMail() {
        return mail;

    public void setMail(String mail) {
        this.mail = mail;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public String execute()

            return SUCCESS;




<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "">

<field name="uname">
    <field-validator type="requiredstring">
       <message>User Name is required.</message>

<field name="age">
   <field-validator type="int">
        <param name="min">15</param>
        <param name="max">25</param>
        <message>Age must be ${min} - ${max}</message>

<field name="mail">
    <field-validator type="requiredstring">
    <message>email required</message>

    <field-validator type="email">
    <message>enter valid email id</message>



<?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>

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.


8 Responses to “Struts2 XML Validations Example, Declarative Validations In Struts”
  1. Arun says:

    Can we configure single validation.xml in struts 2 for all the action being used in our project?

  2. rishi says:

    Is there any possible to apply XML and programmatic validations in same form?……………….

  3. Nitya says:

    How to properly format the error message.Because the messages are displayed on top of the UI controlls?

  4. How to properly format the error message.Because the messages are displayed on top of the UI?

  5. Not able getting the error messages from from LoginEx-validation.xml

    where we need configure

  6. Chakravarthi says:

    Hi Java4s,

    in which situation we will go for declarative validations and in which situation we will go for programatic validaions can u plz clarify?

  7. Test+user says:

    if we have separate java bean class for form inputs instead having a getter and setter in action class then how we implement the declarative validation?

  8. RKRAJ says:

    Good for beginner


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.