Struts 2 Ajax Tutorial with Example

Welcome to the last part of 7 article series of Struts 2 Framework tutorials. In previous article we saw how to implement File Upload functionality in Struts 2. In this article we will see how we can implement Ajax support in a webapplication using Struts2 framework.

AJAX support in Struts 2

Struts 2 provides built-in support to AJAX using Dojo Toolkit library. If you are new to Dojo, you may want to go through the Introduction of DOJO Toolkit.

Struts 2 comes with powerful set of Dojo AJAX APIs which you can use to add Ajax support. In order to add Ajax support, you need to add following JAR file in your classpath:

Also once we add this JAR file, we need to add following code snippet in whatever JSP file we need to add AJAX support.

<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>

First define the taglib sx which we will use to add AJAX enabled tags.


Add this head tag in your JSP between <head> … </head> tags. This sx:head tag will include required javascript and css files to implement Ajax.

AJAX Example: Struts2 Ajax Drop Down

Let us add simple AJAX support in our StrutsHelloWorld web application. We will use the base code that we used in previous articles and add Ajax on top of it.

We will create a drop down which will Autocomplete and suggest the input. For this we will add Dojo support to our webapp.

Step 1: Adding JAR file

As discussed earlier we will add struts2-dojo-plugin.jar in classpath (WEB-INF/lib). Thus, following is the list of required jar files. Note that these jars are needed to run full application including all the samples of previous parts of this tutorial series.

Step 2: Create AJAX Action class

We will create an action class which will get called for our Ajax example. Create a file in net.viralpatel.struts2 package and copy following content into it.

package net.viralpatel.struts2;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.opensymphony.xwork2.ActionSupport;

public class AjaxAutocomplete extends ActionSupport {
	private String data = "Afghanistan, Zimbabwe, India, United States, Germany, China, Israel";
	private List<String> countries;
	private String country;
	public String execute() {
		countries = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(data, ",");

		while (st.hasMoreTokens()) {
		return SUCCESS;
	public String getCountry() {

	public List<String> getCountries() {
		return countries;

	public void setCountries(List<String> countries) {
		this.countries = countries;
	public void setCountry(String country) { = country;

In above code we have created a simple action class with attribute String country and List countries. The countries list will be populated with country names when execute() method is called. Here for this example, we have loaded static data. You may feel free to change this and add data from database.

Step 3: Create JSP

Create JSP file to display Autocomplete textbox for our Ajax action. Create AjaxDemo.jsp in WebContent directory.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
	<sx:head />
	<h2>Struts 2 Autocomplete (Drop down) Example!</h2>
	<sx:autocompleter size="1" list="countries" name="country"></sx:autocompleter>

In above JSP file we have used sx:autocompleter tag to render an autocomplete drop down which users Ajax class to fetch data internally. Note that we have mapped the list attribute with List countries.

Step 4: Creating Struts.xml entry

Add following action entry in Struts.xml file:

<action name="ajaxdemo" class="net.viralpatel.struts2.AjaxAutocomplete">
	<interceptor-ref name="loggingStack"></interceptor-ref>
	<result name="success" type="tiles">/ajaxdemo.tiles</result>
	<result type="tiles">/ajaxdemo.tiles</result>

Notice that we are using Tiles here in this example. You may want to use AjaxDemo.jsp instead of /ajaxdemo.tiles to render the output directly in JSP.

That’s All Folks

Compile and Run the application in eclipse.

Download Source Code

Click here to download Source Code without JAR files (24KB)


Struts2 Framework provides wide variety of features to create a rich web application. In this Struts2 series we saw different aspects of Struts 2 like introduction of struts2, hello world application, validation framework, tiles plugin, strurts2 interceptors, file upload and ajax support.

Get our Articles via Email. Enter your email address.

You may also like...


  1. Ganeshan says:

    Thanks viral for all Struts2 tutorial…………….

  2. manjunath says:

    Hello patel,
    Its better to put Demo link so that we can see demo live and test application ..

    • Viral Patel says:

      Hi Manjunath, Thanks.. Although I always try to add demo links for html, css, javascript tutorials; for Struts its not that simple. You’ll need a running Java container in order to execute your application which is not always feasible. Also there is no reliable Java hosting that can be used to put such live demo. I hope you can download the application and run it in your local machine with easy.

      • manjunath says:

        Its ok Bro.. any how great explanation yar..

      • purnasri says:

        i want the country,state,city 3 dependent dropdown list boxes example by using struts mvc developed using myeclipse

        • krishna says:

          Even I want this, did you solve this? please let me know.

  3. anvit says:

    can u provide jar files also…i need it…

  4. Dave says:

    I try to add exact the same code but it is not working. how AjaxDemo.jsp know which action to trigger in Struts.xml. I did not see any action in JSP link to “ajaxdemo” in Struts.xml?

  5. sri says:


    Can you put on an example showing on how to pass data from one action to another action, using scopemodeldriven or scope interceptors.

    the use case is like this. we use ORM , so on the retrieval entire domain object is populated , say with 20 fields in database . Now in the form I can only update 10 fields , so these 10 fields data only to be updated , but as we are using ORM we need to update the object/table which has all the 20 fields. So if I can carry that 20 fields from one action to another action and then i will update only those fields that are visible in form.

  6. Sujata says:

    good site best example

  7. Wasil says:

    Simply the best on web :)
    Thanks !

  8. Imran Khan says:

    Thanks for Struts Example.
    Best and Simple,
    Thanks Again

  9. Andrew says:

    I learn a lot from this tutorial :D
    Thanks !

  10. Yuvaraj says:

    Is there anyway to achieve pure ajax call with dojo tag? that is list should populate only after I typed any text in field.?

  11. purnasri says:

    use 3 dependent dropdown boxes example in strutsmvc using myeclipse ide

  12. Naman says:

    Hey virat,
    Was a great learning experience, but can u help me to autocomplete from my database so i doesnt need to populate the “list”

  13. Rahul says:

    Hi viral……. you mentioned we can add database content in ajax feature. Like in this case name of countries are displayed. So how could i get name of countries which are in database.

  14. Rahul says:

    hi Viral,
    Your tuts are good but could have been better if you could also provide jar files.

  15. Saumitra Tewari says:

    Hello, I want to know where are declaring action name=”ajaxdemo” and where is your opening action tag.
    New to struts 2.

  16. Manne says:

    Hi Viral,
    In AjaxDemo.jsp, there is a piece of code that went in the body as below( which don’t have any sense here). But seems more html piece of code missing. Can you please clarifiy on this

        <h2>Struts 2 Autocomplete (Drop down) Example!</h2>
        <sx:autocompleter size="1" list="countries" name="country"></sx:autocompleter>
    • Manne says:

      Self Correction:
      Please remove the tag “” in the jsp body and it works well.

      Also to load the countries in the drop down, please use the link as like below which works fine to me.

      link: http://localhost:8080//ajaxdemo.action

  17. Rajdeep says:

    I am getting this error

    Error reading included file template/~~~ajax/controlheader-core.ftl – Class: freemarker.core.Include

  18. Gagandeep Singh says:

    Even I am facing similar issue. On exploring a bit, found its a known issue with struts 2.3.16 and did not exist in 2.3.15. Could someone please confirm is any fix is available by now or is it still an issue. The error I got is :
    Error reading included file template/~~~ajax/controlheader-core.ftl
    The problematic instruction:
    ==> include “/${parameters.templateDir}/${parameters.expandTheme}/controlheader-core.ftl” [on line 23, column 1 in template/xhtml/controlheader.ftl]
    in include “/${parameters.templateDir}/xhtml/controlheader.ftl” [on line 24, column 9 in template/ajax/controlheader.ftl]
    in include “/${parameters.templateDir}/ajax/controlheader.ftl” [on line 23, column 1 in template/ajax/autocompleter.ftl]

  19. Sravya says:

    hai can u please give code for three dynamic dropdowns in struts2

  20. Pravlaya says:

    Hi Viral, can u please post an example of struts2 with html5.

  21. Yogesh says:

    In Struts 2 , my form contains 3 fields, one fields is of type file.
    in struts.xml

    If we submit from normally then control goes to MonitoredMultiPartRequest , but if form sumbiteed using Ajax, then control does not go to MonitoredMultiPartRequest. How to resolve this?

  22. Yogesh says:

    n Struts 2 , my form contains 3 fields, one fields is of type file.
    in struts.xml

     &lt;constant name=&quot;struts.multipart.parser&quot; value=&quot;com.action.MonitoredMultiPartRequest&quot; /&gt;

    If we submit from normally then control goes to MonitoredMultiPartRequest , but if form sumbiteed using Ajax, then control does not go to MonitoredMultiPartRequest. How to resolve this?

  23. wei says:

    I use struts2-dojo-plugin-2.3.20, should be empty, but the sample code include the content below, which one is correct.

    Struts 2 Autocomplete (Drop down) Example!



  24. Newbie says:

    Downloaded the source file and included all the .jar files in lib as mentioned.When I run I get
    HTTP Status 404 – /StrutsHelloWorld
    Please help!Struggling to learn Ajax with struts

  25. goodyzain says:

    Dojo is deprecated. Dojo is deprecated. Dojo is deprecated. I mean the struts-dojo is deprecated. The standalone Dojo is good, the other one is deprecated. Don’t use it, because is deprecated.

  26. Qais says:

    Give Me an error exception
    there is No israel !! >> Free Palestine :)

Leave a Reply

Your email address will not be published. Required fields are marked *