Struts + Spring integration example

Here’s a tutorial to show how to access beans declared in the Spring Ioc container in a web application developed with Apache Struts 1.x.

Download this Struts 1.x + Spring example – Struts-Spring-Hibernate-Example.zip

Spring comes with “Struts-specific” solution for access beans declared in the Spring Ioc container.

  1. Register a Spring’s ready-make Struts plug-in in the Struts configuration file.
  2. Change your Struts action class to extend the Spring’s ActionSupport class, a subclass of the Struts Action class.
  3. The ActionSupport provide a convenient getWebApplicationContext() method for you to access beans declared in Spring Ioc container.

1. Struts + Spring dependencies

To integrate with Struts 1.x, Spring is required the “spring-web.jar” and “spring-struts.jar” libraries. You can download it from Spring web site or Maven.
pom.xml

        <!-- Spring framework --> 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring</artifactId>
		<version>2.5.6</version>
	</dependency>
 
        <dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>2.5.6</version>
	</dependency>
 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-struts</artifactId>
		<version>2.0.8</version>
	</dependency>

2. Register Struts plug-in

In your Struts configuration file (struts-config.xml), register the Spring’s ready-make Struts plug-in – “ ContextLoaderPlugIn“.

struts-config.xml

<struts-config>
    <!-- Spring Struts plugin -->
 	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation"
			value="/WEB-INF/classes/SpringBeans.xml" />
  	</plug-in>
</struts-config>

The “ContextLoaderPlugIn” will handle all the integration work between Struts and Spring. You can load your Spring’s bean xml file into the “contextConfigLocation” property.

SpringBeans.xml

<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-2.5.xsd">
 
	<!-- Beans Declaration -->
	<import resource="com/mkyong/customer/spring/CustomerBean.xml"/>
 
</beans>

3. Spring’s ActionSupport

In Struts Action class, extends the Spring “ActionSupport” class, and get the Spring’s bean via “getWebApplicationContext()” method.

CustomerBean.xml

   	<bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" >
   		<property name="customerDao" ref="customerDao" />
   	</bean>

Struts Action

package com.mkyong.customer.action;
 
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;
 
import com.mkyong.customer.bo.CustomerBo;
import com.mkyong.customer.model.Customer;
 
public class ListCustomerAction extends ActionSupport{
 
  public ActionForward execute(ActionMapping mapping,ActionForm form,
	HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
	CustomerBo customerBo =
		(CustomerBo) getWebApplicationContext().getBean("customerBo");
 
	...
	return mapping.findForward("success");
 
  }
}

Done.

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: car parking()

  • Pingback: water ionizers()

  • Pingback: laan penge online()

  • Pingback: parking()

  • Pingback: laane penge nu()

  • Pingback: 3gp mobile porn()

  • Pingback: tvpackages.net()

  • Pingback: enagic kangen water()

  • Pingback: kangen water()

  • Pingback: alkaline()

  • Pingback: water ionizers()

  • Pingback: free movie downloads()

  • Pingback: stream movies()

  • Pingback: watch movies online free()

  • Pingback: stream movies()

  • Pingback: Blue Coaster33()

  • http://[email protected] Manish

    Hi
    I was using spring frame wok , i have completed almost 50% work , but i want to use struts 2 .is it possible to convert in struts . i mean spring to struts ???
    Please reply me ..
    Thanks a lot

  • Raju

    Can you please send me the direcotry strutcure(not in eclipse) for struts1.x and hibernate and spring integeration to my mail id..
    plzzzzzzzz it is very urgent

    • http://skjd satwik

      gooooooooooooood..

  • manisg
     Java source code here
     XML here
  • Quazi Mohammad Farhan Ali

    Hi, in your struts Spring integration example, Struts Action is tightly coupled with spring ActionSupport class. I think it is better if you had used DelegateActionProxy class instead or you could have used Spring ContextLoaderServlet and contextConfigLocation in web.xml file.

  • Adnen

    Hi when i try deployin application, i have this error :

    Parsing error processing resource path /WEB-INF/struts-config.xml
    java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
    at org.apache.commons.digester.Digester.createInputSourceFromURL(Digester.java:1938)
    at org.apache.commons.digester.Digester.resolveEntity(Digester.java:1615)
    at com.sun.org.apache.xerces.internal.util.EntityResolverWrapper.resolveEntity(EntityResolverWrapper.java:107)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.resolveEntityAsPerStax(XMLEntityManager.java:1018)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1192)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1090)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1003)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at org.apache.commons.digester.Digester.parse(Digester.java:1745)
    at org.apache.struts.action.ActionServlet.parseModuleConfigFile(ActionServlet.java:738)
    at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:687)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:333)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3996)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4266)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:980)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:943)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1203)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:319)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
    21 nov. 2012 11:57:31 >> La servlet action est marqué comme indisponible
    21 nov. 2012 11:57:31 >> La servlet action est marqué comme indisponible
    21 nov. 2012 11:57:31 >> La servlet /StrutsExample a généré une exception “load()”
    javax.servlet.UnavailableException: Parsing error processing resource path /WEB-INF/struts-config.xml
    at org.apache.struts.action.ActionServlet.handleConfigException(ActionServlet.java:769)
    at org.apache.struts.action.ActionServlet.parseModuleConfigFile(ActionServlet.java:743)
    at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:687)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:333)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3996)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4266)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:980)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:943)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1203)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:319)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
    21 nov. 2012 11:57:31 >> La servlet /StrutsExample a généré une exception “load()”
    javax.servlet.UnavailableException: Parsing error processing resource path /WEB-INF/struts-config.xml
    at org.apache.struts.action.ActionServlet.handleConfigException(ActionServlet.java:769)
    at org.apache.struts.action.ActionServlet.parseModuleConfigFile(ActionServlet.java:743)
    at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:687)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:333)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3996)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4266)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:980)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:943)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1203)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:319)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
    21 nov. 2012 11:57:31 >> Failed to register in JMX: javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: com.oberthur.auth.properties.BasicDataSourceFactory]
    21 nov. 2012 11:57:31 >> Failed to register in JMX: javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: com.oberthur.auth.properties.BasicDataSourceFactory]
    21 nov. 2012 11:57:31 >> Démarrage de Coyote HTTP/1.1 sur http-8080
    21 nov. 2012 11:57:31 >> Démarrage de Coyote HTTP/1.1 sur http-8080

    Help me please,
    Thanks

  • sumit

    Hi Mkyong,

    I had requirement that i need to integrate struts tiles with spring Mvc. we are using tiles module from stuts. so please post a example of this kind.

    Thanks
    Sumit

  • Pingback: Strut Spring Integration()

  • Nguyen Trung Hieu

    Urgent, Plz help me!!!

    Unable to initialize Struts ActionServlet due to an unexpected exception or error thrown, so marking the servlet as unavailable. Most likely, this is due to an incorrect or missing library dependency.
    java.lang.NoClassDefFoundError: org/apache/commons/chain/config/ConfigParser
    at org.apache.struts.action.ActionServlet.initChain(ActionServlet.java:1680)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:350)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.Stan

    : java.lang.ClassNotFoundException: org.apache.commons.chain.config.ConfigParser
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    … 14 more

    Servlet /02StrutsSpringIntegration threw load() exception
    javax.servlet.UnavailableException: org/apache/commons/chain/config/ConfigParser
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:402)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1190)
    at org.apache.ca

    http://www.mkyong.com/struts/struts-error-nosuchmethoderror-digester-parseljavaneturlljavalangobject/

    Although, i tried to add all jar files. But still error.

  • rajesh b

    My application in such way that integration of struts2-spring-Hibernate
    so, send me all the Jar files needed to this application,plz
    my mail ID is : (deleted to avoid spam)

    • http://www.mkyong.com mkyong

      This is a Maven project, get the pom.xml in download project and all the dependencies jars at you end.

      • Viswanath

        Hi,

        This is not Struts + Spring Example, it looks struts+spring+hibernate, please post struts+spring example..
        tq

  • Pingback: Struts + Spring + Quartz scheduler integration example()

  • audrey

    Very useful tutorial.
    Thank you.

  • Pingback: Struts 2 + Spring + Quartz scheduler integration example()

  • Pingback: Struts 2 + Spring integration example()

  • Pingback: Spring Tutorials | J2EE Web Development Tutorials()

  • Pingback: Struts Tutorials | Tutorials()