Let us see how to add Spring support (Spring IOC) to a Struts application using Eclipse IDE. I will use the hello world struts project that we created in this tutorial as base reference and step by step we will add Spring support to it.
In this tutorial we will use Spring IOC (Inversion Of Control) mechanism. There will be a business delegate class that will be used to authenticate user. In Login class we will inject this BusinessDelegate class using Spring Injection. Let us start with tutorial.
First, Download required JAR file required for setting Spring support to our Struts application. For this we will need Spring.jar.
Download it from here (Spring.jar, version 1.1, 985kb).
You can download latest JAR file from Spring frameworks download page.
Copy spring.jar file in WEB-INF/lib folder of our struts project.
Now open your struts-config.xml from WEB-INF folder and add following entry for plugin in it. This will add Spring support to your struts project.
Note that in this example we are using Spring framework to instantiate an object of BusinessDelegate class as well as LoginAction class and injecting this object of BusinnessDelegate in LoginAction.
So create a package net.viralpatel.struts.helloworld.business and create a java file BusinessDelegate.java in it.
Copy following code for login validation in BusinessDelegate class.
In type attribute, instead of LoginAction, we have used org.springframework.web.struts.DelegatingActionProxy. This is spring framework class that will delegate the call to action to specific class in ApplicationContext.xml file. Note that path=”/login” should match to name=”/login” entry that we made in ApplicationContext.xml file.
And that’s it. We have just implemented Spring support to our struts application.
You can download war file of struts-spring application with full source here. SOURCE CODE