Ajax Validation

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
  • Pega testing training
    PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • blockchain course
    Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • ethical hacking course
    Ethical Hacking Certification Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • python training
    Python Online 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.

  • Tableau Training
    Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • what is pega
    PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

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