Wednesday, 20 May 2009

Integrating Spring with Struts

Key to the Spring Framework is the freedom to choose what works best for your application. Spring offers you choices when building web applications, too. Although Spring offers its own very capable web framework, you are free to choose another if you’d like and still be able to take advantage of Spring in the other layers of your application.

Advantages of integrating a Struts application into the Spring framework:

1. Spring was explicitly designed to resolve some of the real-world problems of JEE, such as complexity, poor performance, testability, and much more.
2. Second, the Spring framework includes an AOP implementation that lets you apply aspect-oriented techniques to normal object-oriented code.

Both Struts and Spring frameworks have their merits and drawbacks, but most would agree that Struts is still king when it comes to MVC. Many development teams have learned to rely on Struts as the foundation for building quality software under strict deadlines. With so much momentum behind Struts, even development teams that would like to integrate features of the Spring framework don't want to switch to Spring MVC. The good news is that you don't have to. The Spring architecture allows you to connect Struts as your Web framework to Spring-based business and persistence layers.

Below we will see three approaches to integrating Struts applications into the Spring framework and discuss their merits and demerits.

A. Using Spring's ActionSupport class to integrate Struts.
B. Override the Struts RequestProcessor with Spring's DelegatingRequestProcessor.
C. Delegate Struts Action management to the Spring framework.

Whatever technique you use, you will need to add the Spring ContextLoaderPlugin to load the Spring application context for the Struts ActionServlet. Simply add the plug-in to your struts-config.xml file as you would any other plug-in, as shown below:


<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>
</plug-in>



and here is the code for my bean, interface and impl which is common to all three methodologies.


public class Employee implements Serializable{

private Long id;
private String name;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

public interface EmployeeI {

public Employee findEmployeeById(Long id);

public int getNoOfEmployees();
}

public class EmployeeImpl implements EmployeeI{

private Map<Long,Employee> employees = new HashMap<Long,Employee>();

public Employee findEmployeeById(Long id){
System.out.println("Inside findEmployeeById id = "+id);
Employee employee = (Employee)employees.get(id.toString());
System.out.println("Employee found = "+employee);
return employee;
}

public int getNoOfEmployees(){
return employees.size();
}

public Map<Long, Employee> getEmployees() {
return employees;
}

public void setEmployees(Map<Long, Employee> employees) {
this.employees = employees;
}

}


In my Impl Class I am having HashMap which is set at runtime using Spring's getter injection.

I have divided three methodologies into three ariticles as dont want to make it messy. Complete code is provided in the respective articles.

Method A : A. Using Spring's ActionSupport class to integrate Struts.

Method B : B. Override the Struts RequestProcessor with Spring's DelegatingRequestProcessor.

Method C : C. Delegate Struts Action management to the Spring framework.

If you directly want to go the best method. Find out my continuation article,
C. Delegate Struts Action management to the Spring framework.

3 comments:

william said...

Hi! Your blog is simply super. you have create a differentiate. Thanks for the sharing this website. it is very useful professional knowledge. Great idea you know about company background.
Increasing your web traffic and page views Add, add your website in www.directory.itsolusenz.com

Prashant Jalasutram said...

Hi Sneha,

Excellent explanation about integration Struts with Spring.

Your blog is very valuable for Spring resources.

Regards
Prasanth Jalasutram

Freekeswar said...

This a very nice tutorial.

regards,
Eswar.
VaanNila