Struts 2 form validation using annotation

Struts 2 form validation using annotation

To Validate form field in struts 2 application you could directly annotate form field. In this demo you will see how to validate form fields.


  • Create maven project name: Struts2FormValidation. Below is final structure of project:

Struts 2 form validation using annotation

package com.javahonk.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

        @Result(name = "error", location = "/index.jsp"),
        @Result(name = "input", location = "/index.jsp")})
public class FormValidationAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    private String email;

    @Action(value = "/formValidationAction")
    public String execute() throws Exception {
        return ActionSupport.INPUT;

    public String getFirstName() {
        return firstName;

    @RequiredStringValidator(message = "First name is required.")
    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    @RequiredStringValidator(message = "Last name is required.")
    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getEmail() {
        return email;

    @RequiredStringValidator(message = "Email id is required.")
    @EmailValidator(message = "Please enter valid email.")
    public void setEmail(String email) { = email;

  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
        <title>Struts 2 form validation using annotation</title>
        <h1 style="color: green"><s:text name="Struts 2 form validation using annotation" /></h1>
        <s:if test="hasActionErrors()">
            <div id="fieldErrors">
        <s:form action="formValidationAction" namespace="/" method="post" name="strutsForm">
            <s:textfield name="firstName" size="30" maxlength="50" key="First Name"/>
            <s:textfield name="lastName" size="30" maxlength="50" key="Last Name"/>
            <s:textfield name="email" size="30" maxlength="50" key="Email Id"/>            
            <s:submit type="button" key="submit" label="Submit" align="right"/>            
  • pom.xml:
<project xmlns="" xmlns:xsi=""
  <name>Struts2FormValidation Maven Webapp</name>

  • Hopefully you already setup tomcat server with eclipse if not please use this tutorial. To run: Right click on project –> Run As –> Run on Server. You will see below page:

Struts 2 form validation using annotation

  • Click Submit button without entering any data. You will see below validation message on the page:

Struts 2 form validation using annotationStruts 2 form validation using annotation


For more information please read struts 2 tutorial here.

download2 Download Project:  Struts2FormValidation

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *