Ajax Validation

Click to rate this post!
[Total: 0 Average: 0]

Ajax Validation:

Ajax Validation – jsonValidation Interceptor

Offers help to ajax approval. In such case, page won’t be invigorated or reloaded so it will make the execution quick. It is verifiable done utilizing javascript i.e. utilized for the customer side approval.

Steps to perform AJAX
  1. create the form to get input from the user
  2. Inherit the ActionSupport class in your action
  3. Define the validation in validation.xml file
  4. Define result name input for the error message and register the jsonValidationWorkflowStack in struts.xml file

jsonValidation Interceptor

The AJAX approval is performed by jsonValidation interceptor. It is not found in the default stack so we have to characterize it unequivocally. It doesn’t play out any approval itself that is the reason it must be utilized with approval interceptor. It is found in the jsonValidationWorkflowStack, that incorporates jsonValidation, approval and work process interceptors and basicstack.

Aware Interfaces:

Struts 2 Aware interfaces are utilized to place data into the demand, reaction, setting or session question.

The activity class must execute these interfaces to store data with the goal that it can be recovered from other activity class

The four aware interfaces are:

  1. org.apache.struts2.interceptor.SessionAware
  2. org.apache.struts2.util.ServletContextAware
  3. org.apache.struts2.interceptor.ServletRequestAware
  4. org.apache.struts2.interceptor.ServletResponseAware

ServletActionContext class

The ServletActionContext class gives techniques to get HttpServletRequest, HttpServletResponse, ServletContext and HttpSession objects.

It is a helpful class and prefered than ActionContext class.

  1. public static HttpServletRequest getRequest()
  2. public static HttpServletResponse getResponse()
  3. public static ServletContext getServletContext

SessionAware Interface

By the Action class to store the data in the session scope  must be executed Strategy for SessionAware interface ,It contains just a single strategy setSession.

Open and dynamic Method  :

void setSession(Map<String,Object> map)    struts system calls this technique by passing the occurrence of SessionMap class.

SessionMap class

The struts 2 system passes the case of org.apache.struts2.dispatcher.SessionMap.It expands the java.util.AbstractMap class which executes the java.util.Map.SessionMap. There are numerous helpful techniques for SessionMap class.

 Methods of SessionMap class
public Object put(Object key, Object value)
public Object remove(Object key)
public Object get(Object key)
public Set entrySet()
public void invalidate()
public void clear()

ServletContextAware Interface:

Public void setServletContext(ServletContext setting);

Struts 2 with i18n example:

There can be a ton of genuine use of ServletContextAware interface.

The i18n interceptor gives multi-lingual help to your application. It handles setting region for the activity. It can be utilized if client needs to set his/her region and get information as indicated by the region gave. It is found in the defaultStack bydefault, so you don’t need to determine it expressly.

Parameters of i18n interceptor:

There are 2 parameters defined for i18n interceptor. Both are optional.

Parameter Description
parameterName It specifies the name of the HTTP request parameter.

It is set to request_locale bydefault.

attributeName specifies the name of the session key to store the locale.

It is WW_TRANS_I18N_LOCALE bydefault.

i18n interceptor

In this example, we are creating following pages

  1. Login.java
  2. Login_en.properties and Login_hi.properties
  3. struts.xml
  4. index.jsp
  5. login-success.jsp


1) Create the action class


package com.javaspot;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport{

private String name;

public String getName() {

return name;


public void setName(String name) {

this.name = name;


public String execute(){

return SUCCESS;



2) Create properties files


login.msg=Good Morning!



3) Create index.jsp for input


<%@ taglib prefix=”s” uri=”/struts-tags” %>

<s:form action=”login”>

<s:textfield name=”name” label=”Name”></s:textfield>


<s:textfield name=”request_locale” label=”Language Code”></s:textfield>





<?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”>


<package name=”abc” extends=”struts-default” >

<action name=”login” class=”com.javaspot.Login” method=”execute”>

<result name=”success”>/login-success.jsp</result>




4) Create view component


<%@ taglib uri=”/struts-tags” prefix=”s” %>

Welcome, <s:property value=”name”/>


Message is: <s:text name=”login.msg”></s:text>

Zero Configuration by convention

There are two ways to create zero configuration file:

  • By convention
  • By annotation

The steps to create zero configuration file using convention is as follows:

  1. Create input page (optional)
  2. Create the action class
  3. Create view components
create zero configuration file by convention.

In this example,


  1. LoginAction.java
  2. login-success.jsp
  3. login-error.jsp
1) Create index.jsp for input (optional)


<%@ taglib uri=”/struts-tags” prefix=”s” %>

<s:form action=”login”>

<s:textfield name=”name” label=”Name”></s:textfield>

<s:password name=”password” label=”Password”></s:password>

<s:submit value=”login”></s:submit>


 2) Create the action class


package action;

public class LoginAction {

private String name,password;

public String getName() {

return name;


public void setName(String name) {

this.name = name;


public String getPassword() {

return password;


public void setPassword(String password) {

this.password = password;


public String execute(){


return “success”;



return “error”;



3) Create view components


<%@ taglib uri=”/struts-tags” prefix=”s” %>

Welcome,<s:property value=”name”/>


Sorry username or password error!

<jsp:include page=”/index.jsp”></jsp:include>

to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button