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"%>
<html>
  <head> <sj:head/> </head>
  <body>

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

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

</body>
</html>

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.*;

 @Action("/ajaxhello")
 @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" %>

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

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="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="apache-struts-config-example" 
version="3.0">

  <display-name>Example Not Struts Config XML</display-name>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

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="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mcnz</groupId>
  <artifactId>struts-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-convention-plugin</artifactId>
      <version>2.5.10</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    
    <!--  Apache Struts JQuery Plugin Dependency -->
    <dependency>
      <groupId>com.jgeppert.struts2.jquery</groupId>
      <artifactId>struts2-jquery-plugin</artifactId>
      <version>4.0.3</version>
    </dependency>

  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
         <source>1.8</source>
         <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

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
Security
SearchAWS
Close