Tutorial:Struts Spring framework example in Eclipse.

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.


             

Create an xml file called ApplicationContext.xml in WEB-INF directory.

Copy following content in ApplicationContext.xml file.





	
	
	

	    
	
		
	

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.

package net.viralpatel.struts.helloworld.business;

public class BusinessDelegate {
	
	public String validateUser(String userName, String password) {
		if(userName.equals("admin") && password.equals("123")) {
			return "success";
		}
		
		return "failure";
	}
}

Also add a property for BusinessDelegate class in LoginAction and its getter and setter method. This property will get injected by Spring. Copy/Paste following code in your LoginAction.java file.

package net.viralpatel.struts.helloworld.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.viralpatel.struts.helloworld.business.BusinessDelegate;
import net.viralpatel.struts.helloworld.form.LoginForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {
	
	private BusinessDelegate businessDelegate;
	
	public BusinessDelegate getBusinessDeletage() {
		return businessDelegate;
	}

	public void setBusinessDelegate(BusinessDelegate businessDeletage) {
		this.businessDelegate = businessDeletage;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		String target = null;
		LoginForm loginForm = (LoginForm)form; 
		    
		target = businessDelegate.validateUser(loginForm.getUserName(), 
												loginForm.getPassword());
		
		return mapping.findForward(target);
	}
}

Note that LoginAction class is also instantiate using Spring. Hence we will modify entry in struts-config.xml.

	
		
			
			
		
	

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



25 Comments

  • Suresh 22 December, 2008, 3:03

    Thanks Viral… good article…

  • Viral 5 January, 2009, 11:52

    You welcome Suresh :)

  • shripad 1 April, 2009, 12:24

    http://localhost:8080/Struts-Spring/login.do;jsessionid=813C9C5F6B4228C9D4E9B70394AE52F1
    Error :
    The requested resource (Servlet action is not available) is not available.

  • Sandeep 14 April, 2009, 21:59

    Viral, its working with spring 1.1 but not woking with spring 2.x. what changes we have to made to work with spring 2.x

  • Viral Patel 15 April, 2009, 13:56

    Hi Sandeep,
    I am not sure what packages are missing in Spring 2.x. I will check it and update this tutorial after making it compatible with Spring 2.x.

    Thanks

  • Andrea B. 15 April, 2009, 15:16

    I’ve got the same problem with Spring 2.5.6 , i resolved by downloading spring-struts.jar.zip( 25 k) ( http://www.java2s.com/Code/JarDownload/spring-struts.jar.zip )

    now it works!

  • Sandeep 16 April, 2009, 19:16

    Thanks Andrea…its working now after downloading the jar.

  • Madhukar 25 July, 2009, 23:45

    Very nice tutorial, practical and worked easily. Thanks for avoiding loads of theory and adding something real.

  • vennela 18 August, 2009, 5:29

    Trying to access different .css files, but getting No mapping found for HTTP request with URI

  • Viral Patel 18 August, 2009, 12:55

    Hi Vennela, Can you paste here the code snippet of CSS inclusion? I am not able to understand the problem from above statement.

  • sh4m 21 August, 2009, 9:43

    can u add more example?? but only Spring configuration.. without struts. it is possible to do that..?

  • Kiran Hegde 12 February, 2010, 14:18

    Hi dude,
    Please add this line so that the welcome page will show Welcome Admin!

    In LoginAction
    request.setAttribute(“message”, loginForm.getUserName());

    Its there in ur Struts Tutorial :)

  • rajdeo 8 October, 2010, 17:05

    hi
    i have a problem while accessing the struct in eclipse IDE . how to setup the eclipse for struct plz help me immediately

  • reza 21 October, 2010, 15:16

    can i use this tutorial with spring 2.5.x and struts2? thanks

  • Manisha 30 May, 2011, 17:37

    A very helpful tutorial…. Thanks….

  • Prakash Karunanidhi 13 January, 2012, 15:08

    Good!

  • Quazi Mohammad Farhan Ali 22 January, 2013, 14:04

    Hi Viral You have showed best and easy way of spring struts configuration. struts action class is decoupled with spring specific ActionSupport . your way is short and sweet in the sense we need not to configure spring ContextLoderServlet and contextConfigLocation in web.xml file.
    Thank you very much.

  • Arvind Kumar 15 October, 2013, 0:38

    Hi Viral,
    I always visit your blogs. I want to know one thing Struts 1.x is a mvc framework and spring is also a mvc framework. so why we use both framework in one application together.what is advantages of using both together. Please reply ASP with valid reason.

    • Viral Patel 15 October, 2013, 13:44

      Hi Arvind, Spring has many different features other than MVC. In this tutorial we are using Spring’s dependency injection mechanism to manage object lifecycle. You can always use Struts as MVC and Spring for other purpose (Spring Data, Spring Core, Spring Transaction etc.)

  • srinivas 23 October, 2013, 20:12

    How we can decide to which version are using like spring with hibernate and java

  • Rahul 7 November, 2013, 22:49

    Hi,

    viral can you send me spring with struts1 and hibernate sample application.

  • Anant 11 March, 2014, 15:29

    Hi Viral,

    I am getting Nullpointerexception, for the depedency,
    means the dependency doesn’t inject the bean to its object.
    How to resolve this?

    package com.xyz.action;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    import com.xyz.dao.JdbcDao;
    import com.xyz.form.LoginForm;
    import com.xyz.form.RegForm;

    public class LoginAction extends Action{

    private JdbcDao dao;

    public ActionForward execute(ActionMapping mapping,ActionForm form,
    HttpServletRequest request,HttpServletResponse response)throws Exception{

    String target = “failure”;
    LoginForm loginForm =(LoginForm)form;
    HttpSession sess=request.getSession();

    try{
    RegForm rForm=dao.login(loginForm.getUserName(), loginForm.getPassword());
    if(rForm!=null){
    System.out.println(rForm.getDesig());
    sess.setAttribute(“message”, rForm);
    target=”success”;
    }
    }
    catch (Exception e) {
    System.out.println(e);
    }

    return mapping.findForward(target);
    }

    public JdbcDao getDao() {
    return dao;
    }

    public void setDao(JdbcDao dao) {
    this.dao = dao;
    }
    }

    I am getting an exception when I am trying to get the dao.login() method because dependency doesn’t inject properly.

    ApplicationContext.xml

    Please help me out why I am getting Null pointer exception.
    and ur attached code have some issues, it doesn’t extract properly,

    Thanks

  • Anant 11 March, 2014, 15:31

    ApplicationContext.xml

  • kumar 24 June, 2014, 18:12

    useless

  • sandeep 23 September, 2014, 19:29

    COuld you share sample program where I could use ActionSupport of Spring to integrate with struts

Leave a Reply

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

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]