Struts2 Validation using validation.xml Netbeans Example

Struts2 provides different validation methods. In this post basic validation method is described that uses the validation.xml file.
 
File Structure

validation_struts2_file_structure_netbeans

 
—index.jsp—

 
—Register.java—

 
—struts.xml—

 
—Register.validation.xml— (Place this file in the folder where your Register.java is)

 

There are other pages also. Download the complete program.
Validation_Struts2_Mvn_Netbeans.rar

 
OUTPUT

validation_output_eclipse

source: http://struts.apache.org/release/2.0.x/docs/validation.html

xml Validation Struts2 Example

Struts2 provides different validation methods. In this post basic validation method is described that uses the validation.xml file.

File Structure

validation_struts2_file_structure_eclipse
—index.jsp—

Struts 2 Validation Example

 
—Register.java—

—struts.xml—

—Register.validation.xml— (Place this file in the folder where your Register.java is)

 

There are other pages also. Download the complete program.
Validation_Struts2_Mvn_Eclipse.rar

OUTPUT

validation_output_eclipse

source: http://struts.apache.org/release/2.0.x/docs/validation.html

Getting null value of a field while working with struts2 validation through .xml file

This is a small post to provide the solution of a problem of getting null value of a field while working with struts2 validation through .xml file.

Validation content that is entered by the user through .xml file is one of a way for validating fields in struts2 framework.
For this you have to make a file with extension .xml with special name. i.e

Where “class_name” is the name of the class that contain variables that are going to be validate and then add specified coding for validation of the variables in .xml file.

One most important thing that should kept in mind while validation by this method, the class that contains variable should also contain the getter / setter of those variables in the same class. If you do not write getter / setter or there is any mismatch in the filed name and getter /setter then you always get a null value even if you write value in the field in your jsp page.

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.

It is explain as follows.
Example:

 

Note: the validation file name should be carefully written
Format for name is

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

—NewTeacher-validation.xml—(validation file)

—NewTeacher.java—(action class)

—newTeacher.jsp—

—struts.xml—