Apache Struts 7.0.3 is a modern, open-source MVC framework for building enterprise-grade Java web applications. It emphasizes convention over configuration, supports Jakarta EE, and requires Java 17 or higher. Configuring a Struts 7.0.3 application involves setting up the web.xml
deployment descriptor and the Struts configuration file, typically named struts.xml
. Below, we outline the steps to configure these files correctly, followed by an explanation of how Struts integrates with the Spring Framework.
The web.xml
file, located in the /WEB-INF
directory, is the standard deployment descriptor for Java web applications. For Struts 7.0.3, you need to configure the Struts filter to handle incoming requests, replacing the older ActionServlet
used in Struts 1. The following example shows how to set up the Struts FilterDispatcher
to process requests with a .action
URL pattern.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<!-- Struts Filter Configuration -->
<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>*.action</url-pattern>
</filter-mapping>
</web-app>
This configuration uses the StrutsPrepareAndExecuteFilter
, which is the standard controller for Struts 2 and later, including Struts 7.0.3. It intercepts requests ending in .action
and routes them to the appropriate Struts actions. Unlike Struts 1, Struts 7.0.3 does not rely on servlet-based controllers, making it more flexible and aligned with modern Jakarta EE standards.
The struts.xml
file, typically placed in the /WEB-INF
directory or the classpath (e.g., src/main/resources
), defines the Struts application's configuration, including action mappings, results, and interceptors. Below is a basic example of a struts.xml
file for Struts 7.0.3.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="com.example.actions.HelloAction" method="execute">
<result name="success">/WEB-INF/views/hello.jsp</result>
</action>
</package>
</struts>
In this example:
package
element organizes actions under a namespace (/
for root-level actions).action
element maps a URL pattern (hello.action
) to a Java class (HelloAction
) and its execute
method.result
element specifies the view (e.g., a JSP page) to render when the action returns a success
outcome.This configuration is minimal but extensible. Struts 7.0.3 supports annotations as an alternative to XML, allowing developers to define actions directly in Java classes for simpler setups. For example:
@Namespace("/")
@Action(value = "hello", results = {
@Result(name = "success", location = "/WEB-INF/views/hello.jsp")
})
public class HelloAction {
public String execute() {
return "success";
}
}
Apache Struts 7.0.3 integrates seamlessly with the Spring Framework, a popular choice for building modern Java applications due to its robust dependency injection (DI) and aspect-oriented programming (AOP) capabilities. Spring complements Struts by managing the lifecycle of Struts action classes and providing additional features like transaction management and data access.
To integrate Struts 7.0.3 with Spring, you can use the struts2-spring-plugin
, which allows Spring to manage Struts action classes as beans. This enables dependency injection into Struts actions, making applications more modular and testable. To set this up:
pom.xml
:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>7.0.3</version>
</dependency>
ContextLoaderListener
to initialize the Spring application context:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
applicationContext.xml
file in /WEB-INF
to define beans that Struts actions can use. For example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.example.services.HelloServiceImpl" />
<bean id="helloAction" class="com.example.actions.HelloAction" scope="prototype">
<property name="helloService" ref="helloService" />
</bean>
</beans>
public class HelloAction {
private HelloService helloService;
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
public String execute() {
// Use helloService for business logic
String message = helloService.getMessage();
return "success";
}
}
jakarta.servlet
instead of javax.servlet
).While Struts 7.0.3 is a modernized framework, Spring (particularly Spring Boot) has become the preferred choice for many new Java web projects due to its lightweight nature and extensive ecosystem. Struts is often used in legacy systems or where its specific MVC model is preferred. For new projects, developers may opt for Spring MVC or Spring Boot alone unless Struts' plugin architecture or action-based workflow is required.
For further details, consult the Apache Struts documentation for Struts 7.0.3 specifics and the Spring Framework documentation for integration details.