Integrating Spring with your web application
Using Spring Framework facilitates good, extensible design by promoting interface driven programming, ease of testing through Inversion of Control and supporting various test frameworks and much more. Integrating Spring in a Struts2 based web application is a logical next step. In this post, we will integrate the Spring Framework in the Struts2 web application started in this post – Starting Struts2 web application development (using Maven2 and Eclipse)
Integrating Spring with the existing Struts2 application is pretty straight forward. We need the following modifications to the existing project to enable Spring support.
-
Make sure that the
struts2-spring-plugin
is included as a dependency in yourpom.xml
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.1.8.1</version> </dependency>
-
Add Spring’s
ApplicationContext
to configure and manage Struts2Action
beans and/or any other beans used in the application.<?xml version = "1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- Example of Struts2 action instantiated by Spring --> <bean id="helloAction" class="com.rajandesai.example.webapp.action.HelloAction" singleton="false" /> </beans>
-
Modify
web.xml
to add spring support. Changes are highlighted in the following code.<?xml version="1.0" encoding="UTF-8"?>
<web-app id="web-app" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Simple Hello World App using Struts2 and Spring</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param><filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!– Listeners –>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app> -
Modify
struts.xml
to use reference to thehelloAction
bean defined in theapplicationContext.xml
. We will also change thestruts.objectFactory
property to usespring
object factory instead ofstruts
.<?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> <include file="struts-default.xml"/> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.objectFactory" value="spring" /> <package name="helloworld" namespace="/hello" extends="struts-default"> <default-interceptor-ref name="defaultStack"/> <action name="hello" class="helloAction"> <result>/WEB-INF/example/hello.jsp</result> </action> <!-- Add additional "example" package actions here. --> </package> <!-- Add addition packages and configuration here. --> </struts>
-
We are now ready to deploy and test the application. As before, run
mvn jetty:run
from the project directory (wherepom.xml
exists).
And then in the browser, try these sample URLs to test the application:-
Hello World: http://localhost:8080/hello/hello.action
-
Hello World with the twist: http://localhost:8080/hello/hello.action?name=John%20Doe&zip=02451&age=30&gender=M
Conclusion
Integrating Spring with the Struts2 application is very straight forward. We are now ready to explore advantages Spring brings to the application design. Next, we will integrate Hibernate with this application and also see Spring simplifies writing unit tests for Struts2 actions.
Source code is available here. -
Hello World: http://localhost:8080/hello/hello.action
Discussion
No comments yet.