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.
Spring is a popular web framework that allows for simple integration with a wide range of basic online operations. So, why do we need Spring when we already have Struts2? Spring is more than just an MVC framework; it has many features that Struts does not.
Consider dependency injection, which may be used in any framework. In this article, we'll walk through a simple example of how to combine Spring with Struts2.
⭐Setting Up Project
📎To begin, add the following items to the project's build path from the Spring installation. You can get the most recent version of Spring Framework and install it from here.
📎Finally, place struts2-spring-plugin-x.y.z.jar in your WEB-INF/lib directory from your struts lib directory. If you use Eclipse, you may encounter the following exception:
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
📎To resolve this issue, navigate to the Marker tab and right-click on each class dependency one by one before selecting Quick fix to publish/export all dependencies. Finally, check the marker tab for any dependent conflicts.
⚒️Setting up web.xml
Let us now configure the web.xml for the Struts-Spring integration as follows:
Code
<?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>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
</web-app>
📎The listener that we have configured is vital to remember here. To load the spring context file, the ContextLoaderListener is necessary. The applicationContext.xml file is Spring's configuration file, and it must be placed at the same level as the web.xml file.
⚒️Setting up User.java
Write a simple action class called User.java that has two properties: firstName and lastName.
package com.codingninjas.struts2;
public class User {
private String firstName;
private String lastName;
public String execute() {
return "success";
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
⚒️Creating applicationContext.xml
Let us now generate the spring configuration file applicationContext.xml and instantiate the User.java class. As previously stated, this file should be located in the WEB-INF subdirectory.
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "userClass" class = "com.codingninjas.struts2.User">
<property name = "firstName" value = "Soham" />
<property name = "lastName" value = "Medewar" />
</bean>
</beans>
📎As shown above, we configured the user bean and injected the values "Soham" and "Medewar" into it. We've also named this bean "userClass" so that we may utilize it elsewhere. Let us now construct the User.jsp file in the WebContent folder.
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts 2</title>
</head>
<body>
<h1>This is the page from Struts2 with Spring integration</h1>
<s:form>
<s:textfield name = "firstName" label = "First Name"/><br/>
<s:textfield name = "lastName" label = "Last Name"/><br/>
</s:form>
</body>
</html>
📎The User.jsp file is rather simple. It just serves one purpose: it displays the values of the user object's firstName and lastName. Finally, we'll use the struts.xml file to connect all of the entities.
<?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.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "user" class="userClass"
method = "execute">
<result name = "success">/User.jsp</result>
</action>
</package>
</struts>
📎It is vital to remember that we are referring to the class using the id userClass. This means that the User class's dependency injection is handled by spring.
📎To produce a War file, right-click on the project name and select Export > WAR File. After that, place this WAR in Tomcat's webapps directory. Finally, launch Tomcat and navigate to http://localhost:8080/HelloWorldStruts2/User.jsp. This will result in a form of the webpage on localhost on port 8080. This webpage will have two input fields for firstName and lastName.