Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
⭐Setting Up Project
2.1.
⚒️Setting up web.xml
2.2.
⚒️Setting up User.java
2.3.
⚒️Creating applicationContext.xml
3.
Frequently Asked Questions
3.1.
Is struts 2 the same as struts?
3.2.
What are the Struts 2 core components?
3.3.
What is struts 2 used for?
3.4.
What is the model in struts?
3.5.
Is Struts 2 thread safe?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Struts 2 & Spring Integration

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

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

Setting Up

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

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 the Struts 2 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 about Struts 2 and the steps to integrate spring in the struts 2 application. 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 image
Live masterclass