Skip to content
javahacks.dev
Struts

Struts 2 vs Spring MVC and Why Teams Choose to Migrate

Compare Struts 2 and Spring MVC on programming model, configuration, security history, testing and ecosystem, and when staying on Struts 2 is the right call.

5 min read
Summarize in AI
Flat illustration of a controller, a service and a repository stacked above a database, with the Spring, Apache Maven, PostgreSQL and Swagger logos.

Both frameworks are web MVC frameworks on the servlet API, and they were designed years apart. The choice today is rarely a clean slate, so the comparison is between starting on one and moving off the other.

Programming model

Struts 2: an action is a class with fields for request data, an execute() method and result names resolved in struts.xml. Interceptors form stacks that populate, validate and wrap the action. A new instance serves each request.

java
public class LoginAction extends ActionSupport {
    private String username;
    private String password;
    public String execute() { return auth.login(username, password) ? SUCCESS : INPUT; }
    // getters and setters
}

Spring MVC: a controller is a bean with handler methods whose parameters Spring resolves from the request, and whose return value is a view or a body.

java
@PostMapping("/login")
String login(@Valid LoginForm form, BindingResult errors) {
    return errors.hasErrors() || !auth.login(form) ? "login" : "redirect:/home";
}

The handler-method style removes the state from fields and makes dependencies visible through constructors, as explained in DispatcherServlet.

Comparison

AspectStruts 2Spring MVC
Configurationstruts.xml plus annotationsAnnotations and Java config, Boot auto-configuration
Request dataAction fields via params interceptorMethod parameters and annotations
View technologyJSP with Struts tags, FreeMarkerThymeleaf, JSP, JSON bodies, any
Expression languageOGNLSpEL and JSP EL
TestingStrutsTestCase, rarely usedMockMvc, slices, plain unit tests
RESTREST pluginFirst class, @RestController
EcosystemPlugins, smallerSpring Boot, Security, Data, Cloud

Security

The most important difference for many teams is history. Struts 2 has had critical remote code execution vulnerabilities rooted in OGNL evaluation of request data, and breaches caused by unpatched versions are well known. The framework has since added allowlists and stricter defaults, see OGNL safely. Spring MVC has had its own CVEs, and it does not evaluate request parameters as expressions by default. Either way, the practice that matters is patching quickly and following the hardening list.

Integration and ecosystem

Spring MVC lives inside Spring Boot: a starter, embedded server, Actuator, Security, Data, OAuth2, observability, Testcontainers support. Most new libraries document Spring first. Struts 2 works with Spring through a plugin, but the integration points are fewer, see Struts with Spring. This is the main reason teams cite for leaving.

People

Spring skills are common in the market, and Struts experience is concentrated in teams that maintain older systems. A framework few new hires know raises onboarding cost and bus-factor risk.

When to stay on Struts 2

  • The application is stable, has good tests and a patch process.
  • The roadmap is short, or the business is replacing the system anyway.
  • The cost of rewriting views and tests exceeds the risk.

Stay current when you stay: upgrade within Struts and keep the container patched.

When to move

  • You are adding REST endpoints, single sign-on, observability or containers.
  • You cannot patch quickly enough to meet your security obligations.
  • New developers struggle with the stack.

Then follow the incremental route in migrating to Spring MVC.

How concepts map across

A migration is easier to plan when each Struts idea has a counterpart. The mapping below covers the pieces that every application has:

Struts 2Spring MVC
Action class with fields@Controller method with parameters or a request object
execute() returning a result nameA method returning a view name, a ModelAndView or a body
struts.xml action mappings@GetMapping and @PostMapping on methods
Value stack and OGNLModel attributes, Thymeleaf or JSP with EL
Interceptor stacksFilters and HandlerInterceptor
XML validation and validate()Bean Validation annotations on a request class
Tiles layoutsThymeleaf layouts or fragments
ActionSupport messagesMessageSource and resource bundles
*.action URLsClean paths such as /orders/{id}

The first row hides the largest change. A Struts action is created for each request and holds request data in its own fields, and a Spring controller is a singleton that receives data as arguments. Code that stores state in action fields needs thought, since the same state in a controller field would be shared by every request.

Run both frameworks in one application

A migration with a date at the end tends to fail. A gradual one works, because both frameworks can serve different URLs from the same WAR. Map the Struts filter to the old extension and let Spring's dispatcher take the rest:

XML
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
</filter-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

New pages are written in Spring MVC, and an old page moves when someone changes it for another reason. A redirect from the old .action URL to the new path keeps bookmarks and search results working. Session data is the sticking point, since each framework stores it its own way, so move shared state, such as the logged-in user, into the servlet session under plain keys that both read.

Estimate the work from facts

Count before promising a schedule. The inputs are the number of actions, the number of custom interceptors, the number of result types other than JSP, the number of validation files and the number of pages that use OGNL beyond simple property access. Pages with a few fields move quickly, and pages with dynamic forms, wizards or custom tags take far longer. A sample of five typical actions, migrated and tested by the people who will do the rest, gives a per-action cost that is better than any estimate made from a distance.

What teams report after moving

The gain that shows up first is hiring and onboarding, since far more developers know Spring than Struts. The second is security, because the framework is no longer the source of a stream of advisories, and the third is tooling: tests, IDE support and the ecosystem of starters. The cost is a period of running two frameworks, and the loss of a few pieces of behaviour that nobody wrote down, such as a default interceptor that trimmed whitespace from every parameter. Find those by comparing responses between the old and new pages for the same input, and write the missing behaviour as an explicit filter or converter.

The case against migrating now

A stable internal application with a handful of users, behind a VPN, patched to the latest Struts 6 or 7 line, with a team that knows it, can stay where it is. The money is better spent elsewhere. Migrate when the framework blocks something you need, such as a Java upgrade, or when keeping it patched costs more each year than the move.

Where this does not apply

For a brand-new project the question is not between these two. Compare Spring MVC with the options in Spring Boot versus Micronaut or Quarkus instead. The reference is Apache Struts, and Spring MVC documents the other side.

Also inSpring MVC
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.