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.

Methods
  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
Methods
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

 Login.java:

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_en.properties

login.msg=Good Morning!

 Login_hi.properties

login.msg=Suprabhat!

3) Create index.jsp for input

 index.jsp

<%@ 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>

–>

<s:submit></s:submit>

</s:form>

 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=”abc” extends=”struts-default” >

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

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

</action>

</package>

</struts>

4) Create view component

 login-success.jsp

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

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

<br>

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,

index.jsp

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

 index.jsp

<%@ 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>

</s:form>

 2) Create the action class

 LoginAction.java

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(){

if(password.equals(“struts”)){

return “success”;

}

else

return “error”;

}

}

3) Create view components

 login-success.jsp

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

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

login-error.jsp

Sorry username or password error!

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

Drop Us A Query

Trending Courses
  • Python and Django 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 Online 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.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification 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).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


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