Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts 2 Validation Using XML File Tutorial

  • submit to reddit

In this example we will see how we can perform validations using XML validation file. The naming convention of the XML validation file should be ActionClass-Validation.xml. Here our Action Class name is "" and the XML validation file name is "Login-Validation.xml".

The Login-Validation.xml file contains the following code.

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

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

The field element contains the name of the form property that needs to be validated. The filed-validator element inside the field element contains the type of validation that needs to be performed.

Here you can either specify the error message directly using the message element or you can use the properties file to define all the errror messages and use the key attribute to specify the error key.

Note the properties file should also have the same name as the Action class.

The Login Action class contains the following code.

public class Login extends ActionSupport {

    private String userName;
    private String password;

    public Login() {

    public String execute() {
        return SUCCESS;

    public String getUserName() {
        return userName;

    public void setUserName(String userName) {
        this.userName = userName;

    public String getPassword() {
        return password;

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

The login.jsp page contains the following code.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
<s:head />
    <s:form action="LoginAction">
        <s:textfield name="userName" label="User Name" />
        <s:password name="password" label="Password" />
        <s:submit value="Login" />

The <s:head /> tag is used to include the required css and js file for the selected theme. By default the xhtml theme is used.

Execute the example and click the Login button without entering the user name and password the following page will be displayed.

Enter a user name and password and click the Login button the following success page will be displayed.

You can download the Struts 2 Validation using XML file example by clicking the download link.

Source :Download
Source + Lib :Download
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Yuxiong Bai replied on Sat, 2013/01/12 - 10:51pm

There is an error in the article. Name of validation file must be :


ActionClass-Validation.xml is wrong.

 "v" not "V"

Vijay Kumar replied on Tue, 2013/06/18 - 9:01am

if you get an error like validation is not working, then change the code in Login-validation.xml file

<!DOCTYPE validators PUBLIC

  "-//Apache Struts//XWork Validator 1.0.3//EN"


instead of this this code,

<!DOCTYPE validators PUBLIC 

"-//OpenSymphony Group//XWork Validator 1.0.2//EN"


Ligz Anghao replied on Thu, 2013/08/22 - 2:20am

 clear and clean, helpful

Andrew Kani replied on Sun, 2013/11/17 - 8:01pm

Very nice tutorials, I am a computer science student and I am trying this now with my friend. The instructions given here are pretty clear, but we will let you know of any questions.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.