differences between Struts1 and Struts2
Struts1 and Struts2 are both web application frameworks based on the MVC (Model-View-Controller) architecture. Struts2 is the successor to Struts1 and was designed to address many of the shortcomings and limitations of Struts1. Below are some key differences and improvements that make Struts2 a better choice for modern web application development.
Table of Contents
Key Differences
- 1. Â Action Classes :
- Struts1 : Actions must extend org.apache.struts.action.Action, which ties them to the framework.
- Struts2 : Actions are simple POJOs (Plain Old Java Objects) that implement
com.opensymphony.xwork2.Action or even just a method returning a string. This promotes better separation of concerns and easier unit testing.
- 2. Â Servlet Dependency :
- Struts1 : Action classes are tightly coupled with the servlet API.
- Struts2 : Actions are not tied to the servlet API, which allows for more flexible and cleaner code.
- 3. Â Configuration :
- Struts1 : Uses struts-config.xml for configuration.
- Struts2 : Uses struts.xml with more flexible and modular configuration options.
- 4. Â Threading Model :
- Struts1 : Actions are singletons and must be thread-safe, which can lead to concurrency issues.
- Struts2 : A new action instance is created for each request, eliminating threading issues and simplifying development.
- 5. Â Form Beans :
- Struts1 : Requires separate form beans that extend ActionForm.
- Struts2 : Uses the action properties as form properties, reducing boilerplate code.
- 6. Â Validation :
- Struts1 : Uses separate validate methods in form beans or Validator Framework.
- Struts2 : Uses a more flexible validation framework with annotations and XML configurations.
- 7. Â Interceptors :
- Struts1 : Limited to request processors.
- Struts2 : Provides a powerful interceptor stack for preprocessing and postprocessing logic, allowing for reusable and modular code.
- 8. Â Tag Libraries :
- Struts1 : Uses its own tag library.
- Struts2 : Integrates with JSP 2.0 and supports a more extensive set of tag libraries.
- 9. Â AJAX Support :
- Struts1 : Limited built-in support.
- Struts2 : Built-in support for AJAX through various plugins.
Example Comparison
Below is a simple comparison demonstrating how to create an action in both Struts1 and Struts2.
Struts1 Example
java
package com.example.action;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setAttribute("message", "Hello, World!");
return mapping.findForward("success");
}
}
xml
<struts-config>
<form-beans>
<form-bean name="helloForm" type="com.example.form.HelloForm"/>
</form-beans>
<action-mappings>
<action path="/helloWorld" type="com.example.action.HelloWorldAction">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
</struts-config>
jsp
<!-- success.jsp -->
<html>
<head>
<title>Struts1 Hello World</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Struts2 Example
java
package com.example.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String message;
public String execute() {
message = "Hello, World!";
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
xml
<struts>
<package name="default" extends="struts-default">
<action name="helloWorld" class="com.example.action.HelloWorldAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
jsp
<!-- success.jsp -->
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2 Hello World</title>
</head>
<body>
<h1><s:property value="message"/></h1>
</body>
</html>
Explanation
- 1. Â Struts1 Action Class : The action class extends Action and uses the servlet API to handle the request. The configuration is done in struts-config.xml.
- 2. Â Struts2 Action Class : The action class extends ActionSupport and does not depend on the servlet API. The configuration is done in struts.xml, which is more flexible and easier to manage.
Conclusion
Struts2 offers several improvements over Struts1, including a more flexible configuration, better separation of concerns, easier integration with other frameworks, and a more powerful and reusable mechanism for handling cross-cutting concerns through interceptors. These enhancements make Struts2 a better choice for modern web application development.