Weekend -Special offer upto 50% off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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 classThe 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 classThe 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 InterfaceBy 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 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.
ParameterDescription
parameterNameIt specifies the name of the HTTP request parameter.It is set to request_locale bydefault.
attributeNamespecifies the name of the session key to store the locale.It is WW_TRANS_I18N_LOCALE bydefault.
i18n interceptorIn 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.propertieslogin.msg=Good Morning! Login_hi.propertieslogin.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:submit>s:submit>s:form>
 struts.xml:
xml version="1.0" encoding="UTF-8" ?>><struts><package name="abc" extends="struts-default" ><action name="login" class="com.javaspot.Login" method="execute"><result name="success">/login-success.jsp4) Create view component login-success.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Welcome, 
Message is: Zero Configuration by conventionThere 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" %>
 2) Create the action class
 LoginAction.javapackage 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";}elsereturn "error";}}
3) Create view components
 login-success.jsp<%@ taglib uri="/struts-tags" prefix="s" %>Welcome,login-error.jspSorry username or password error!