Struts2 Spring 3 Integration Example


This is 8 of 8 part of struts 2 tutorial.

Tutorial Content:

In this post,you will see how to integrate Spring 3 and Struts 2. Now first question comes here ,why you want to integrate spring with struts 2? Spring provides some features which are not available in struts 2.Most powerful among them is dependency injection.To learn more about dependency injection,you can refer dependency injection in spring link.

Create project named “Struts2Spring3IntegrationExample”.
First of all, you need to copy following files to WEB-INF/lib and add them to project’s build path. You can download and install latest version of Spring Framework from http://www.springsource.org/download

  • org.springframework.asm-3.1.1.RELEASE
  • org.springframework.beans-3.1.1.RELEASE
  • org.springframework.context-3.1.1.RELEASE
  • org.springframework.core-3.1.1.RELEASE
  • org.springframework.expression-3.1.1.RELEASE
  • org.springframework.web-3.1.1.RELEASE
  • org.springframework.web.servlet-3.1.1.RELEASE

Finally copy struts2-spring-plugin-2.3.1.2 to WEB-INF/lib from your downloaded struts2 jars.

For configuring all above jars and struts 2 jars in your eclipse ide please refer configuring struts 2 link.

Project structure:

Web.xml:

Copy following content in web.xml.

Here you can note that we have configured listener.
org.springframework.web.context.ContextLoaderListner  is required to load spring configuration file.By default the applicationContext.xml file will be used for doing the Spring bean configuration and it must be same level as web.xml.

Action class:

Create action class named HelloWorld.java under package org.arpit.javaPostsForLearning in src folder.

copy following content to HelloWorld.java.
Now we will configure above action file in applicationContext.xml.

applicationContext.xml

Create applicationContext.xml in WebContent->WEB_INF. Copy following content to applicationContext.xml.

JSPs:

We will create one jsp named “Welcome.jsp” under WebContent folder. Copy following code into Welcome.jsp

struts.xml:

Create struts.xml in src folder. Copy following content in struts.xml

Now you can note here that In tag,we have set class attribute same as id of bean configured in applicationContext.xml.

Run Application:

Right click on project->run as->run on server.
copy http://localhost:8080/Struts2Spring3IntegrationExample/HelloWorld link to  browser and press enter.

Source Code :

click to begin
7.3MB .zip

Related Posts

  • 07 August

    Struts 2 ajax example

    Table of ContentsTutorial Content:Ajax Support in struts 2:Extending above example :Adding another action file:Adding new JSP file:Run application again:Source code: This is 7 of 8 part of struts 2 tutorial.In this tutorial we will see how we can use ajax in our webApplication in struts 2 framework. Tutorial Content: Introduction to struts 2 configuring struts […]

  • 29 July

    File Upload in struts 2

    Table of ContentsTutorial Content:Action class:JSPs:Web.xml:Run project:Source code: This is 6 of 8 part of struts 2 tutorial.In this tutorial we will learn how to upload file to server in struts 2 and also how save uploaded file on directory of server machine. Tutorial Content: [crayon-60f982b03b5da056760773/] Struts 2 file upload component can be used to upload […]

  • 27 July

    Struts 2 Interceptors with Example

    Table of ContentsTutorial Content:Interceptors :Goal :Interceptor classes:Struts.xml:Action:JSP:Web.xml:Run project:Source code: This is 5 of 8 part of struts 2 tutorial.In this post,We will learn about interceptors and creating our own interceptor. Tutorial Content: Introduction to struts 2 configuring struts 2 in eclipse Struts 2 hello world example Login page with validation in struts 2 Struts 2 […]

  • 24 July

    Login page with validation in struts 2

    Table of ContentsTutorial Content:Web.xml:JSP:Action class:Struts.xml:The ResourceBundle:Run Project:Source code: This is 4 of 7 part of struts 2 tutorial. Tutorial Content: Introduction to struts 2 configuring struts 2 in eclipse Struts 2 hello world example Login page with validation in struts 2 Struts 2 interceptors with example File upload in struts 2 Struts 2 ajax example […]

  • 18 July

    struts 2 hello world example

    Table of ContentsTutorial Content:The web.xml file:Action Class:JSP:The struts.xml file:Source code: This is 3 of 8 part of struts 2 tutorial.It provides struts 2 hello world example. Tutorial Content: Introduction to struts 2 configuring struts 2 in eclipse Struts 2 hello world example Login page with validation in struts 2 Struts 2 interceptors with example File […]

  • 16 July

    Configuring struts 2 in eclipse

    This is 2 of 8 part of struts 2 tutorial.In this part,we will configure struts 2 in eclipse. Tutorial Content: Introduction to struts 2 configuring struts 2 in eclipse Struts 2 hello world example Login page with validation in struts 2 Struts 2 interceptors with example File upload in struts 2 Struts 2 ajax example Struts […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.