Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
jsonValidation Interceptor
3.
Steps to Perform AJAX Validation
4.
Example to Perform AJAX Validation
4.1.
Create index.jsp for Input
4.2.
Create the Action Class
4.3.
Create the Validation File
4.4.
Create struts.xml
4.5.
Create View Component
5.
Frequently Asked Questions
5.1.
What type of property of the AJAX () method specifies?
5.2.
How do you validate a form?
5.3.
What is AJAX form submission?
5.4.
What does AJAX stand for?
5.5.
Is AJAX a framework?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

By Ajax Validation (Built-in Validators with Ajax)

Introduction

Ajax validation is supported by Struts 2. The performance will be quick because the page won't be updated or reloaded in this scenario. Javascript is utilized implicitly to carry out the client-side Validation.

An AJAX (asynchronous HTTP) request is made using the ajax() function. All jQuery AJAX techniques use the ajax() function. This approach is typically used when other procedures cannot be employed.

shows the ajax validation

jsonValidation Interceptor

shows the picture of json validation interceptor

JsonValidation interceptor handles the AJAX validation. Since it is absent from the default stack, we must precisely specify it. It must be used with a validation interceptor because it cannot conduct any validation independently. It may be found in the jsonValidationWorkflowStack, which includes the primary stack, workflow interceptors, and JSON validation.

Steps to Perform AJAX Validation

The following are the easy methods to implement AJAX Validation:

  • Create a form to collect user input.
  • In your action, inherit the ActionSupport class.
  • In the validation.xml file, specify the Validation.
  • Set up the jsonValidationWorkflowStack and define the result name input for the error message in the struts.xml file.

Example to Perform AJAX Validation

In this instance, four pages are being created:

  • To support business logic, indexer.java
  • Regist.jsp is used to receive user input.
  • To use validators that are packaged, use register-validation.xml.
  • struts.xml is used to specify the action's interceptors and outcomes.
  • For the view component, use welcome.jsp.

Create index.jsp for Input

Utilizing the struts UI tags, this JSP website generates a form. It gets the user's name, password, and email address.

index.jsp

<%@ taglib  uri="/struts-tags" prefix="s"%>  
<%@ taglib uri="/strts-dojo-tags" prefix="d"%>  
<html>  
<head>  
	<d:head/>  
	</head>  
	<body>  
		<marquee>Rgstrtn Frm.............</marquee>  
 
		<s:form action="rgstr">  
		<s:textfield name="nm" label="Username"></s:textfield>  
		<s:textfield name="ml" label="Email ID"></s:textfield>  
		<s:password name="psswrd" label="Password"></s:password>  
		<d:submit validate="tu" type="image" src="register-now.jpg">  
		</d:submit>  
	</s:form>  
 
	</body>  
</html> 

Create the Action Class

The execute function is overridden in this action class, which derives from the ActionSupport class.

RegisterAction.java

package mypack;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {
  private String name, password, email;

  //setters and getters  

  public String execute() {
    return "success";
  }
}

Create the Validation File

In this case, the Validation is carried out utilizing bundled validators.

Register-validation.xml

<?xml version="1.0" encoding="UTF-8"?>  
   
<!DOCTYPE validators PUBLIC  
 "-//OpenSymphony Group//XWork Validator 1.0.2//EN"  
 "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
 
   <validators>  
         
   <field name="nm">  
   <field-validator type="rqurdstrng">  
   <message>Name is blank?</message>  
   </field-validator>  
   </field>  
         
   <field name="ml">  
   <field-validator type="rqurdstrng">  
   <message>Email is blank?</message>  
   </field-validator>  
   <field-validator type="email">  
   <message>Please enter an acceptable email ID</message>  
   </field-validator>  
   </field>  
         
   <field name="psswrd">  
   <field-validator type="rqurdstrng">  
   <message>Password is blank?</message>  
   </field-validator>  
   <field-validator type="strnglngth">  
   <param name="mnLngth">5</param>  
   <param name="mxLngth">10</param>  
   <message>Password is less than 5 or greater than 10?</message>  
   </field-validator>  
         
   </field>  
         
   </validators>  

Create struts.xml

This XML file specifies a workflow stack interceptor and a different outcome with the name input. jsonValidatorWorkflowStack.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
 
		<package name="a" extends="struts-default">  
 		
		<action name="register" class="mypack.Register">  
		<interceptor-ref name="jsonValidationWorkflowStack"></interceptor-ref>  
 		
		<result name="success">/welcome.jsp</result>  
		<result name="input">/index.jsp</result>  
		</action>  
 		
		</package>  
</struts>     

Create View Component

It is a straightforward JSP file that shows the user's information.

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
 
Welcome, <s:property value="name"/>  

Frequently Asked Questions

What type of property of the AJAX () method specifies?

The ajax() function in jQuery sends an asynchronous HTTP request or an AJAX request. Parameters: Below is a list of potential values: type is utilized to identify the kind of request. It specifies the URL where the appeal should be delivered.

How do you validate a form?

Basic Validation The form must first be reviewed to ensure that all required fields are filled in. All that would be necessary is a simple loop over the form's lots to check for data. Data Format Validation Next, the entered data must be examined for accuracy of form and content.

What is AJAX form submission?

You may send data in the background using AJAX form submission, eliminating the need to reload webpages to view updates. The user experience is much improved as a result.

What does AJAX stand for?

XML and JavaScript are asynchronous. Asynchronous JavaScript And XML are referred to as AJAX. In a nutshell, it involves contacting servers via the XMLHttpRequest object. JSON, XML, HTML, and text files are just a few forms in which it can transmit and receive data.

Is AJAX a framework?

An Ajax framework is a cross-browser framework or library that helps programmers create rich online applications using Ajax.

Conclusion

Finally, you have reached the article's conclusion. Congratulations!! You gained knowledge of the Table Per Subclass in this blog. You now have mastery over Table Per Subclass.

Are you eager to read more articles on Table Per Subclass? Coding Ninjas cover you, so don't worry. View more topics on Coding ninjas.

Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Thank you
Live masterclass