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.

Steps:

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

Struts 2 form validation using annotation

  • FormValidationAction.java
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;

@Results({
        @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;

    @Override
    @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) {
        this.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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Struts 2 form validation using annotation</title>
        <s:head/>
    </head>
 
    <body>
        <h1 style="color: green"><s:text name="Struts 2 form validation using annotation" /></h1>
        <s:if test="hasActionErrors()">
            <div id="fieldErrors">
                <s:actionerror/>
            </div>
        </s:if>               
        <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"/>            
        </s:form>
    </body> 
</html>  
  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javahonk</groupId>
  <artifactId>Struts2FormValidation</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Struts2FormValidation Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16.3</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.16.3</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>Struts2FormValidation</finalName>
  </build>
</project>

  • 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 *