<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this tutorial we will discuss about struts 2 Ajax Call tag with using struts dojo plugin in our application.<br />
Struts uses the DOJO framework for the AJAX tag implementation. First of all, to proceed with this example, you need to add struts2-dojo-plugin-2.3.15.jar to your classpath.</p>
<p>For this exercises, let us create <i><b>ajax.jsp</b></i> as follows:</p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;%@ taglib prefix="sx" uri="/struts-dojo-tags"%>; 
<;html>; 
<;head>; 
<;title>;Ajax Tag Struts2 | dineshonjava.com<;/title>; 
<;s:head />; 
<;sx:head />; 
<;/head>; 
<;body>; 
 <;s:form>; 
 <;sx:autocompleter label="Favourite Colour" 
 list="{'red','green','blue'}" />; 
 <;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="2000">; 
 Initial Content 
 <;/sx:div>; 
 <;br/>; 
 <;sx:tabbedpanel id="tabContainer">; 
 <;sx:div label="Tab 1" href="%{#url1}">;Tab 1<;/sx:div>; 
 <;sx:div label="Tab 2" href="%{#url2}">;Tab 2<;/sx:div>; 
 <;/sx:tabbedpanel>; 
 <;/s:form>; 
<;/body>; 
<;/html>; 
</pre>
<div id="ads-id" align="center"></div>
<p><i><b>select.jsp</b></i></p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;html>; 
<;head>; 
<;title>;Form Tag Struts2 | dineshonjava.com<;/title>; 
<;s:head />; 
<;/head>; 
<;body>; 
 <;s:form action="login.action">; 
 <;s:select name="username" label="Username" 
 list="{'Mike','John','Smith'}" />; 
 
 <;s:select label="Company Office" name="mySelection" 
 value="%{'America'}" 
 list="%{#{'America':'America'}}">; 
 <;s:optgroup label="Asia" 
 list="%{#{'India':'India','China':'China'}}" />; 
 <;s:optgroup label="Europe" 
 list="%{#{'UK':'UK','Sweden':'Sweden','Italy':'Italy'}}" />; 
 <;/s:select>; 
 
 <;s:combobox label="My Sign" name="mySign" 
 list="#{'aries':'aries','capricorn':'capricorn'}" 
 headerKey="-1" 
 headerValue="--- Please Select ---" emptyOption="true" 
 value="capricorn" />; 
 <;s:doubleselect label="Occupation" name="occupation" 
 list="{'Technical','Other'}" doubleName="occupations2" 
 doubleList="top == 'Technical' ? 
 {'I.T', 'Hardware'} : {'Accounting', 'H.R'}" />; 
 
 <;/s:form>; 
<;/body>; 
<;/html>; 
</pre>
<p><i><b>welcome.jsp</b></i></p>
<pre class="highlight"><;%@ 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">; 
<;title>;Form Tag Struts2 | dineshonjava.com<;/title>; 
<;/head>; 
<;body>; 
<;b>;Welcome to Ajax Call in Struts2<;/b>; 
<;/body>; 
<;/html>; 
</pre>
<p>success.jsp</p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
 
 <;s:form action="employee">; 
 <;s:radio label="Gender" name="gender" list="{'male','female'}" />; 
 <;s:checkboxlist label="Job Types" name="jobtypes" 
 list="{'Software','Hardware','Networking','Marketing'}" />; 
 <;/s:form>; 
</pre>
<p><b>Strus2 Configuration file</b><br />
<i><b>struts.xml</b></i></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8" ?>; 
<;!DOCTYPE struts PUBLIC 
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
 "http://struts.apache.org/dtds/struts-2.0.dtd">; 
 
<;struts>; 
 <;constant name="struts.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;action name="ajaxtag" class="com.dineshonjava.struts2.action.FormTagAction">; 
 <;result name="success">;/ajax.jsp<;/result>; 
 <;/action>; 
 <;action name="count1" class="com.dineshonjava.struts2.action.FormTagAction" method="tab1">; 
 <;result name="success">;/select.jsp<;/result>; 
 <;/action>; 
 <;action name="count2" class="com.dineshonjava.struts2.action.FormTagAction" method="tab2">; 
 <;result name="success">;/success.jsp<;/result>; 
 <;/action>; 
 <;action name="count3" class="com.dineshonjava.struts2.action.FormTagAction" method="tab3">; 
 <;result name="success">;/welcome.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 <;/struts>; 
</pre>
<p><b>Create Action Class:</b></p>
<pre class="highlight">package com.dineshonjava.struts2.action; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class FormTagAction extends ActionSupport { 
 
 private static final long serialVersionUID = 1L; 
 
 public String execute(){ 
 return SUCCESS; 
 } 
 public String tab1(){ 
 return SUCCESS; 
 } 
 public String tab2(){ 
 return SUCCESS; 
 } 
 public String tab3(){ 
 return SUCCESS; 
 } 
} 
</pre>
<p><i><b>web.xml</b></i></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2FormTags<;/display-name>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 <;welcome-file-list>; 
 <;welcome-file>;index.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
<p>Let us now go through this example one step at a time.</p>
<p>First thing to notice is the addition of a new tag library with the prefix sx. This (struts-dojo-tags) is the tag library specifically created for the ajax integration.</p>
<p>Then inside the HTML head we call the sx:head. This initializes the dojo framework and makes it ready for all AJAX invocations within the page. This step is important &#8211; your ajax calls will not work without the sx:head being initialized.</p>
<p>First we have the autocompleter tag. The autocompleter tag looks pretty much like a select box. It is populated with the values red, green and blue. But the different between a select box and this one is that it auto completes. That is, if you start typing in gr, it will fill it with &#8220;green&#8221;. Other than that this tag is very much similar to the s:select tag which we covered earlier.</p>
<p>Next, we have a date time picker. This tag creates an input field with a button next to it. When the button is pressed, a popup date time picker is displayed. When the user selects a date, the date is filled into the input text in the format that is specified in the tag attribute. In our example, we have specified dd/MM/yyyy as the format for the date.</p>
<p>Right click on the project name and click <b>Export >; WAR</b> File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access</p>
<p><b>URL <i>http://localhost:8080/doj/ajaxtag. </i></b></p>
<p>This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/1212-1.png" border="0" /></div>
<p><b>After 2000 ms delay one ajax call action &#8220;count3&#8221; and display following</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/2121.png" border="0" /></div>
<p><b>When click on &#8220;tab2&#8221; ajax call &#8220;count2&#8221; action and display following.</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/3131.png" border="0" /></div>
<p> ;</p>
</div>
<p><b>Download Source Code + Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2AjaxTags.zip?attredirects=0&;d=1" target="_blank" rel="noopener">ajaxtagstruts2example.zip</a></b></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/the-form-tags-in-struts2/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts2-spring-3-integration-example/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/the-form-tags-in-struts2/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts2-spring-3-integration-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '356'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…