struts 2 email validation example

The email validator watches that given field has substantial email address. It works just if field isn't blank.

Parameters of email validator

There is just a single parameter characterized for email validator.

ParameterDescription
fieldNamespecifies the field name that will be approved. It is required in Plain-Validator only.

Example of email validator

  1. <validators>  
  2.    <!- -  Plain Validator Syntax - - >  
  3.     <validator type="email">  
  4.          <param name="fieldName">email</param>  
  5.              <message>Please enter a valid email address</message>        
  6.          </validator>  
  7.       
  8. </validators>  
  1. <validators>  
  2.     <!- -  Field-Validator Syntax - - >  
  3.     <field name="email">  
  4.           <field-validator type="email">  
  5.           <message>Please enter a valid email address.</message>          
  6.        </field-validator>  
  7.     </field>  
  8.   
  9. </validators>  

Full case of email validator

1) Create index.jsp for information

This jsp page makes a structure utilizing struts UI labels. It gets name, secret word and email id from the client.

index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <html>  
  3. <head>  
  4. <STYLE type="text/css">  
  5. .errorMessage{color:red;}  
  6. </STYLE>  
  7. </head>  
  8. <body>  
  9.   
  10. <s:form action="register">  
  11. <s:textfield name="email" label="Enter Email Id"></s:textfield>  
  12. <s:submit value="register"></s:submit>  
  13. </s:form>  
  14.   
  15. </body>  
  16. </html>  

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

RegisterAction.java
  1. package com.javacodegeeks;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Register extends ActionSupport{  
  6. private String email;  
  7.   
  8.   
  9. public String getEmail() {  
  10.     return email;  
  11. }  
  12.   
  13.   
  14. public void setEmail(String email) {  
  15.     this.email = email;  
  16. }  
  17.   
  18.   
  19. public String execute(){  
  20.     return "success";  
  21. }  
  22.   
  23. }  

3) Create the validation file

Here, we are using bundled validators to perform the validation.

Register-validation.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   <!DOCTYPE validators PUBLIC   
  4.         "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
  5.         "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  6.   
  7.         <validators>  
  8.           
  9.         <field name="email">  
  10.         <field-validator type="requiredstring">  
  11.         <message>Email Id can't be blank</message>  
  12.         </field-validator>  
  13.         <field-validator type="email">  
  14.         <message>Please enter correct email id</message>  
  15.         </field-validator>  
  16.           
  17.         </field>  
  18.           
  19.           
  20.           
  21.         </validators>  

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.

struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.   
  5. <package name="default" extends="struts-default">  
  6. <action name="register" class="com.javacodegeeks.Register">  
  7. <result name="input">index.jsp</result>  
  8. <result>welcome.jsp</result>  
  9. </action>  
  10.   
  11. </package>  
  12.   
  13. </struts>      
  14.      
  15.    





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition