Saturday, April 27, 2013

Basic form Validation using Struts2 Framework with validate() method and ActionSupport class

In this Tutorial we are performing server side validation using Struts2 framework with validate() method and for using this method our java class must extend ActionSuppot class.

Project Structure in Project Explorer:
Download Struts2 Jars

Struts2 jars setting, for More Details Read How to set Struts2 Environment

register.jsp
create a user input page.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
      pageEncoding="ISO-8859-1"%>  
      <%@ taglib prefix="s" uri="/struts-tags"%>  
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Register</title>  
 </head>  
 <body bgcolor="brown">  
      <h1>Registration</h1>  
      <hr>  
            <s:form action="register.action" method="post" >  
            <s:textfield name="name" label="Name" size="20" />  
            <s:textfield name="age" label="Age" size="20" />  
            <s:textfield name="course" label="Course" size="20" />  
            <s:submit label="Register" align="center" />  
         </s:form>  
 </body>  
 </html>  

success.jsp
create a user's success page.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib prefix="s" uri="/struts-tags"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>success</title>  
 </head>  
 <body bgcolor="lightblue">  
 <center><h3>  
 Hello! <s:property value="name"/></h3></center>  
 </body>  
 </html>  

ContactAction.java
create a Action java class. Make sure that this java class must extend ActionSupport class else you'll not be able to use validate() method.
 package in.blog.webideaworld;  
 import com.opensymphony.xwork2.ActionSupport;  
 @SuppressWarnings("serial")  
 public class ContactAction extends ActionSupport {  
      private String name;  
      private int age;  
      private String course;  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public int getAge() {  
           return age;  
      }  
      public void setAge(int age) {  
           this.age = age;  
      }  
      public String getCourse() {  
           return course;  
      }  
      public void setCourse(String course) {  
           this.course = course;  
      }  
      public String execute() throws Exception{  
           return SUCCESS;  
      }  
      public void validate(){  
           if ( getName().length() == 0 )       
                addFieldError( "name", "Name is required." );  
           if ( getAge() < 18 || getAge() >50 )  
                addFieldError( "age", "Age is required and must be between 18 and 50" );  
           if ( getCourse().length() == 0 )  
                addFieldError( "course", "Course is required." );  
      }  
 }  

struts.xml
set result name="success" for success.jsp and name="input" for register.jsp for validation messages to show up on input page.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
   <struts>   
   <package name="default" namespace="/" extends="struts-default">  
     <action name="register" class="in.blog.webideaworld.ContactAction" method="execute" >  
     <result name="success">success.jsp</result>  
     <result name="input">register.jsp</result>  
     </action>  
   </package>  
   </struts>  

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>Struts2_form_validate</display-name>  
  <welcome-file-list>  
   <welcome-file>register.jsp</welcome-file>  
   </welcome-file-list>  
  <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.FilterDispatcher  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

Download Code

View in Browser: 
checking validation:

success page: 

1 comment:

Sarthak Goel said...

nice post....keep it up :)

Popular Posts