Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
⭐Introduction
2.
⭐Setting Up Application
2.1.
🛠️Setting up ajax.jsp
2.2.
🛠️Setting up select.jsp
2.3.
🛠️Setting up welcome.jsp
2.4.
🛠️Setting up success.jsp
3.
⭐Struts 2 Configuration File
3.1.
⚙️Struts.xml
3.2.
⚙️Create Action Class
3.3.
⚙️Web.xml
4.
Frequently Asked Questions
4.1.
Is struts 2 the same as struts?
4.2.
What are Struts 2's core components?
4.3.
What is struts 2 used for?
4.4.
What is the model in struts?
4.5.
Is Struts 2 thread safe?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Struts 2-The Ajax Tags

Author soham Medewar
0 upvote

⭐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. 

STRUTS

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

🛠️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>

⭐Struts 2 Configuration File

configuration

⚙️Struts.xml

<?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.codingninjas.struts2.action.FormTagAction">
            <result name="success">/ajax.jsp</result>
        </action>
        <action name="count1" class="com.codingninjas.struts2.action.FormTagAction" method="tab1">
            <result name="success">/select.jsp</result>
        </action>
        <action name="count2" class="com.codingninjas.struts2.action.FormTagAction" method="tab2">
            <result name="success">/success.jsp</result>
        </action>
        <action name="count3" class="com.codingninjas.struts2.action.FormTagAction" method="tab3">
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
</struts>

⚙️Create Action Class

package com.codingninjas.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

/**
* @author Ganesh Medewar
*
*/
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;
	}
}

⚙️Web.xml

<?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>


Let's now walk through this example step-by-step:

🔑The new tag library with the prefix sx is the first item that stands out. The tag library for the ajax integration was written specifically for this (struts-dojo-tags).

🔑Then we call the sx:head within the HTML head. This starts the Dojo framework and prepares it for any incoming AJAX requests on the page. This step is crucial because without initializing the sx:head, your ajax calls will not function.

🔑The autocompleter tag comes first. The autocompleter tag resembles as chosen box quite closely. Red, Green, and Blue values are present in it. But this one differs from a choose box in that it autocompletes. In other words, if you start typing gr, "green" will be filled in. Other than that, this tag and the s:select tag we previously discussed are pretty similar.

🔑The date and time picker comes next. This tag adds a button next to an input field. A popup date and time picker appear when the button is touched. The date is entered into the input text in the format defined by the tag attribute when the user picks a date. In our example, we've set the date's format to dd/MM/yyyy.

🔑To produce a War file, perform right-click on the project name and select Export > WAR File. After that, deploy this WAR to the webapps directory of Tomcat.

Frequently Asked Questions

Is struts 2 the same as struts?

Struts 1 supports distinct Request Processors (lifecycles) for each module, but all of the module's Actions must share the same lifecycle. Struts 2 allows you to create various lifecycles for each Action using Interceptor Stacks. As needed, custom stacks can be generated and utilized with various Actions.

What are Struts 2's core components?

Struts2's main components are Action, Interceptors, and Result pages. Struts2 provides numerous methods for creating Action classes and configuring them using struts.

What is struts 2 used for?

Apache Struts 2 is a free and open-source web application framework used to create Java EE web applications. It extends the Java Servlet API in order to encourage developers to employ a model-view-controller (MVC) architecture.

What is the model in struts?

Struts advocates Model 2 application architectures, which are a version of the model-view-controller (MVC) design pattern. Using Struts to build a complicated web application can help it be more maintainable.

Is Struts 2 thread safe?

Struts2 Action classes are thread-safe because an object is instantiated for every request to handle it.

Conclusion

In this article, we have discussed the Ajax tags in the Struts 2 application. Also, we have developed a small application using the Ajax tag. That’s all from the article.

Be curious

You can refer to the introduction to spring boot, the 19 best python frameworks, and flask introduction to learn more about different frameworks. 

That's the end of the article. I hope you all like this article. 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Thank You
Live masterclass