⭐Introduction
Apache Struts 2 is a free and open-source web application framework used to create Java EE web applications. To encourage developers to employ a model-view-controller design, it uses and enhances the Java Servlet API.
In this article, we will learn about the ajax tags in the struts 2. So without any further delay, let us get started!
⭐Setting Up Application
In this article, we will go over the struts 2 Ajax Call tag in conjunction with the struts dojo plugin in our application.
Struts implement AJAX tags using the DOJO framework. To begin, add struts2-dojo-plugin-2.3.15.jar to your classpath before proceeding with this example.
🛠️Setting up ajax.jsp
Code
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Ajax Tag Struts2</title>
<s:head />
<sx:head />
</head>
<body>
<s:form>
<sx:autocompleter label="Pick a favourite color"
list="{'green','blue','red'}" />
<br />
<sx:datetimepicker name="deliverydate" label="Delivery Date"
displayFormat="dd/MM/yyyy" />
<br />
<s:url id="url1" value="/count1" />
<s:url id="url2" value="/count2" />
<s:url id="url3" value="/count3" />
<sx:div href="%{#url3}" delay="1500">
Start
</sx:div>
<br/>
<sx:tabbedpanel id="tabContainer">
<sx:div label="Tab 1" href="%{#url1}">First Tab</sx:div>
<sx:div label="Tab 2" href="%{#url2}">Second Tab</sx:div>
</sx:tabbedpanel>
</s:form>
</body>
</html>
🛠️Setting up select.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts2 Form Tag | codingninjas.com</title>
<s:head />
</head>
<body>
<s:form action="login.action">
<s:select name="username" label="Username"
list="{'Rohan','Satish','Rahul'}" />
<s:select label="Company Office" name="mySelection"
value="%{'America'}"
list="%{#{'America':'America'}}">
<s:optgroup label="Asia"
list="%{#{'Russia':'Russia','India':'India','Japan':'Japan'}}" />
<s:optgroup label="Europe"
list="%{#{'UK':'UK','Sweden':'Sweden','Italy':'Italy'}}" />
</s:select>
<s:combobox label="My Sign" name="mySign"
list="#{'gemini':'gemini','leo':'leo'}"
headerKey="-1"
headerValue="Select One" emptyOption="true"
value="leo" />
<s:doubleselect label="Occupation" name="occupation"
list="{'Other','Technical'}" doubleName="occupations2"
doubleList="top == 'Technical' ?
{'I.T', 'Hardware'} : {'Accounting', 'H.R'}" />
</s:form>
</body>
</html>
🛠️Setting up welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<titleStruts2 Form Tag | codingninjas.com</title>
</head>
<body>
<b>This is Ajax Call | Struts2</b>
</body>
</html>
🛠️Setting up success.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="employee">
<s:radio label="Gender" name="gender" list="{'female','male'}" />
<s:checkboxlist label="Job Types" name="jobtypes"
list="{'Networking','Software','Marketing','Hardware'}" />
</s:form>