Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Struts2 Spring integration


May 15, 2021 Struts2



Spring is a popular Web framework that provides easy integration with many common Web tasks. S o the question is, when we have Struts2, why do we need Spring? B ecause Spring is more than just an MVC framework, it provides many other good things that are not available in Struts. F or example, dependency injection can be useful for any framework. In this chapter, we'll use a simple example to learn how to integrate Spring and Struts2.
First, you need to add the following files to the build path for Spring's project. You can http://www.springsource.org/download the latest version of the Spring framework from the website.

  • org.springframework.asm-x.y.z.M(a).jar

  • org.springframework.beans-x.y.z.M(a).jar

  • org.springframework.context-x.y.z.M(a).jar

  • org.springframework.core-x.y.z.M(a).jar

  • org.springframework.expression- x.y.z.M(a) .jar

  • org.springframework.web- x.y.z.M(a) .jar

  • org.springframework.web.servlet- x.y.z.M(a) .jar

Finally, add struts2-spring-plugin-x.y.z.jar from your struts lib directory to your WEB-INF/lib. I f you use Eclipse, you may encounter an anomaly java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener. T o solve this problem, you should right-click on class dependencies in the Marker tab and quickly fix them to publish/export all dependencies. F inally, make sure that there are no dependency conflicts available under the Marker tab.

Struts2 Spring integration

Now let's set up a web page for the Struts-Spring .xml, as follows:

<?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 focus here is on the listeners we configure. L oading the spring context file requires ContextLoaderListener. Spring's profile is called the applicationContext .xml file, and it must be placed at the same level .xml the web file.
Let's create a simple action .java User, which has two properties - firstName and lastName.

package cn.w3cschool.struts2;

public class User {
   private String firstName;
   private String lastName;

   public String execute()
   {
      return "success";
   }

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   public String getLastName() {
      return lastName;
   }

   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
}

Now, create a spring profile applicationContext .xml and instantiate the User .java class. A s mentioned earlier, this file should be under the WEB-INF folder:

<?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="cn.w3cschool.struts2.User">
      <property name="firstName" value="Michael" />
      <property name="lastName" value="Jackson" />
   </bean>
</beans>

As shown above, we have configured the user bean, and we have injected the values Michael and Jackson into the bean. W e also gave this bean the name "userClass" so that we could use it again elsewhere. Next, let's create a User user in the WebContent .jsp:

<%@ 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>Hello World</title>
</head>
<body>

   <h1>Hello World From Struts2 - 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>

User .jsp file is simple. I t is used for one purpose only: to display the values of the user object firstname and lastname. F inally, let's use the struts .xml file to put all the entities together.

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

The important thing to note is that we use id userClass to reference classes, which means that we use spring to do dependency injection for the User class.

Now, right-click on the project name, and then click "Export" and "WAR File" to create a WAR file. T he WAR file is then deployed in Tomcat's webapps directory. Finally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/User.jsp display the following interface:

Struts2 Spring integration

We've now seen how to combine two great frameworks, which ends the Struts-Spring integration chapter.