Fork me on GitHub
<< back to Core Developers Guide Edit on GitHub

Basic Validation

Let’s configure a basic validation workflow, step by step.

Step 1

Create the input form.

	<title>Struts2 Showcase - Validation - Basic</title>


<div class="page-header">
	<h1>Basic validation Example</h1>

<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">

				<b>What is your favorite color?</b>


			<s:form method="post">
				<s:textfield label="Name" name="name"/>
				<s:textfield label="Age" name="age"/>
				<s:textfield label="Favorite color" name="answer"/>
				<s:submit cssClass="btn btn-primary"/>

Step 2

Create the Action class.

public class QuizAction extends ActionSupport {

	private static final long serialVersionUID = -7505437345373234225L;

	String name;
	int age;
	String answer;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public int getAge() {
		return age;

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

	public String getAnswer() {
		return answer;

	public void setAnswer(String answer) {
		this.answer = answer;

Step 3

Create the validators. The


format is either





    Add the following DOCTYPE declaration as first line of your XXX-validation.xml file:
    <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "">
    <field name="name">
        <field-validator type="requiredstring">
            <message>You must enter a name</message>
    <field name="age">
        <field-validator type="int">
            <param name="min">13</param>
            <param name="max">19</param>
            <message>Only people ages 13 to 19 may take this quiz</message>

Step 4

Make sure there is a result for “input” listed in the struts.xml for when the validation fails:

<action name="HelloWorld" class="tutorial.HelloWorld">
<result name="success">/tutorial/createconfirm.jsp</result>
<result name="error">/tutorial/error.jsp</result>
<result name="input">/tutorial/create.jsp</result>

If you don’t have this, you’ll get a: “No result defined for action and result input” error