Struts 2 Validation

Struts 2 is a very powerful open source web application framework. It was originally created by
Craig McClanahan and donated to the Apache Foundation in May 2000. It is also called Jakarta Struts.

It has several features, following are the features of the Struts 2

  • Simple POJO based Actions
  • Simplified testability
  • Thread Safe
  • AJAX Support
  • jQuery Plugin
  • Dojo Plugin (deprecated)
  • AJAX Client Side Validation
  • Template Support
  • Support for different result types
  • Easy to extend with Plugins
  • REST Plugin (REST based Actions, Extension-less URLs)
  • Convention Plugin (Action Configuration via Conventions and Annotations)
  • Spring Plugin (Dependency Injection)
  • Hibernate Plugin
  • support in Design
  • JFreechart Plugin (Charts)
  • jQuery Plugin (AJAX Support, UI Widgets, Dynamic Table, Charts)
  • Rome Plugin (RSS Feeds)

The feature of Struts2 that attracts me the most is the validation part of the struts 2 . If you do not use the struts 2 validation for the form validation purpose you have to use the  JavaScript  , but using JavaScript is a very lengthy and complex process and you have to write a large code to validate a single field. so if you are working with struts2 , it is a best option to use struts validation in place of  JavaScript or AJAX because struts2 do it easy for you.

Example:

Note: the validation file name should be carefully written

Format for name is

-validation.xml

example NewTeacher-validation.xml file should be kept in the folder where your action file is located

—NewTeacher-validation.xml—(validation file)

true
ID is required
                
true
FirstName is required
                
---NewTeacher.java---(action class)
package teacheraction;
import com.opensymphony.xwork2.ActionSupport;
public class NewTeacher extends ActionSupport{
    private String teacher_id;
    private String teacher_firstname;
    private String teacher_lastname;
     //getter and setter
public String execute()
{
return "success";
}

—newTeacher.jsp—

<%@taglib uri="/struts-tags" prefix="t" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

---struts.xml---
teacher/newTeacher.jsp
teacher/newTeacher.jsp
success/success.jsp
error/error.jsp