About NewTechnoBuzz
Contact Us

Tuesday, July 8, 2014

Struts 2 vs Spring 3 MVC

Today, Apache Struts 2 and Spring MVC are one of the most popular Java web frameworks. I have worked on both the frameworks and want to share some of the information about both the frameworks by comparing their features.

In Struts, the object that handles a request and process it and then sends back the response is an Action while in Spring MVC, that object is referred to as Controller. Actions and Controllers are pretty much the same thing – they take input, process it, and return the response.

The one major design difference in between them is that by default, Struts 2 Actions are instantiated every time a request is made, whereas Spring MVC, by default creates a singleton of Controller. Spring MVC Controllers are created once and held in memory/shared across all requests. Note, you can change this behavior (scope) to request, session or application. This is a major difference to keep in mind when designing applications that need to be thread-safe, database oriented, or other share-able transactions.

Struts 2 Actions:

The basic Struts 2 Action looks very similar to any normal POJO but with an execute method:
package com.gsms.web.action;
import com.opensymphony.xwork2.Action;
public class EmployeeAction implements Action {

    public String execute() {
       return SUCCESS;

This method just returns the name of the result as defined in the struts.xml configuration file. The struts framework will check the XML for the configured return path and handle forwarding to the proper view (usually a JSP page).

Spring MVC Controllers:

The Spring MVC controller is tagged with @Controller annotation. Then you should use the @RequestMapping annotation to specify what URL patterns the controller will respond to. You can use this annotation at the class level or method level or a combination to achieve your needs.
package com.gsms.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
public class HelloWorldController {
  @RequestMapping(method = RequestMethod.GET)
  public String addNewEmployee() {
    return "hello";
  @RequestMapping(method = RequestMethod.POST)
  public String WelcomeEmpoyee(@RequestParam("name") String name, Model model) {
    String message = "Hi " + name + "!";
    model.addAttribute("message", message);
    return "hi";

Free Search Engine Submission

Struts 2 Configs:

As you can see, the MVC concept is pretty similar between both frameworks. Now, lets compare the configurations required.

Struts 2 only requires a single "struts.xml" config file to configure the framework. A typical action/result routing configuration looks like this:

<action name="EmployeeAction" 
   <result name="success">/pages/success.jsp</result>;
   <result name="input">/pages/input.jsp</result>;
   <result name="fail">/pages/failure.jsp</result>;

In the example above, we returned the "success: result. So this Struts 2 would forward us to success.jsp as the view.

Spring MVC Config:

Spring MVC requires a "servlet-context.xml" file. If we use annotations, then there is not need to configure controller in XML file. We just need to do some configurations in context file to enable annotations and Spring do it automatically. Here’s snippetts from the file:
<!-- Configures the annotation programming model -->
    <mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view. Use this to setup a default action -->
    <mvc:view-controller path="/" view-name="welcome"/>
<!-- Resolves view names to protected .jsp files within the /WEB-INF directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>


Both the frameworks are good. I would suggest reviewing the exact needs of your application before choosing a framework.
In case, you have any query or question, please feel free to post a comment.