Use the Struts jQuery plugin to simplify Ajax request cycles example

The Struts 2 Ajax Template is deprecated, so those interested in an asychronous request-response cycles with the popular Java web framework must look elsewhere. For many, the Struts 2 jQuery plugin has proven itself a more than capable replacement.

Struts & Ajax

This Apache Struts jQuery plugin tutorial will show you how to implement an Ajax-based interaction that can be deployed to any JavaEE compliant application server and will work within any modern web browser. The basic steps required to use the Struts jQuery plugin in any standard Java EE or Jakarta EE web project are as follows:

  1. Build a Maven based Java web project project, preferably within an IDE like Eclipse
  2. Create a file named ajax-link.jsp to trigger the jQuery-based, Ajax request-response cycle
  3. Code a simple Struts 2 action named AjaxHelloAction on the server to handle the Struts jQuery plugin request
  4. Create a file named results.jsp to provide the HTML fragment returned as part of the Struts Ajax response
  5. Add a web.xml file to the Java web project to configure the Apache Struts framework
  6. Update the Maven POM file to leverage the Struts jQuery plugin
  7. Run the Struts 2 jQuery Ajax app on a Java web server such as Tomcat or Jetty

The Struts jQuery custom tags

To use Struts JQuery tags within a JSP, the struts-jquery-tags taglib directive must be added at the top of the JSP. Furthermore, the <sj:head> tag must be included in the HTML head element. Then the jQuery Struts plugin tags can be used to create Ajax based links to Struts actions on the server and to specify DIV elements on the page to update when an asynchronous response must be rendered.

Learn Apache Struts 2 Quickly

Here’s how to learn the fundamentals of Struts 2.5:

Follow these steps and you’ll be an Apache Struts 2 expert in no time!

In the following JSP, an Ajax based link is created that passes the String @cameronmcnz to the server. The results returned then update the <div> element on the page named ajax-results.

Struts jQuery plugin

The Struts jQuery Ajax example app.

The full code for the file named ajax-link.jsp is as follows:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
  <head> <sj:head/> </head>

  <s:url var="ajaxLink" value="/ajaxhello.action">
    <s:param name="handle"> @cameronmcnz </s:param>
  <sj:a id="link1" href="%{ajaxLink}" targets="ajax-results">
    Say Hello to @cameronmcnz

  <div id="ajax-results">
    <h5>JQuery Struts Ajax result will go here</h5>


The Struts 2.5 Action class

To handle the incoming Ajax request, a Struts Action class is created. It’s worth noting that there is nothing special about this component in terms of Ajax or jQuery. This is a standard Struts Action class created according to the Struts 2.5 API. The only Ajax or jQuery Struts related elements used in this application appear in the ajax-link.jsp file. The rest of the application is pure Apache Struts.

package com.mcnz.struts;
import org.apache.struts2.convention.annotation.*;

 @Result(name = "success", location = "/results.jsp")
 public class AjaxHelloAction {

  public String execute() {
    System.out.print("The handle is: " + handle);
    return "success";

 private String handle;
 public String getHandle() { return handle; }
 public void setHandle(String h) { handle = h; }


The Apache Struts Ajax response

The response to the Ajax based request is generated through another server-side component named ajax-results.jsp. This JSP simply renders an HTML fragment which uses standard Struts 2 tags to pull the property named handle out of the AjaxHelloAction class.

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html; charset=UTF-8" %>

Hello <s:property value="handle"/> !!!

The Struts programming part of this example is complete. In order to run this example on the server, two configuration steps must be completed. The Maven POM file must be updated with the required Apache Struts and JQuery plugin dependencies, and the Struts 2 filter must be configured in the web.xml file.

Struts 2.5 and the web.xml file

All Apache Struts 2 applications must include a reference to the StrutsPrepareAndExecuteFilter component in the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 

  <display-name>Example Not Struts Config XML</display-name>

Struts JQuery Maven dependencies

The following XML file described the Maven dependencies a Java web project must include in order to run this Ajax based Struts JQuery tutorial:

<project xmlns="" 

    <!--  Apache Struts JQuery Plugin Dependency -->


Run the Struts 2 JQuery Ajax app

With the JSP files coded, the action class class compiled and the Maven and XML configuration files updated, you can run the Struts JQuery app and experience first hand the joy of integrating Ajax into Struts based applications.

Ajax Struts jQuery

It is easy to include Ajax in a Struts app.


App Architecture
Software Quality
Cloud Computing