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—

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
	Struts 2 Validation Example


Struts 2 Validation Example

 
—Register.java—

package org.apache.struts.helloworld.action;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {
	/**
	 * @author ABHISHEK
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String email;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	 public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String execute() 
	    {
	        return "success";   
	    }
}

—struts.xml—





	
	

        
        
		
			/index.jsp
		

		
			index.jsp
			/Success.jsp
			/ValidationFailed.jsp
		
	

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







    
	
                Name is required
        
    
        
    
        
                Email is required
        

        
                 Enter valid Email
        
    
        



 

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