To my surprise, Struts 2 seems to be completely different than Struts 1 framework, because some of the most familiar stuff like ActionForm, struts-config.xml, and Action classes are changed in Struts 2 framework. Struts 2 has also done good job on removing direct dependency of Action classes on Servlet API e.g. HttpServletRequest and HttpServletResponse, which makes testing easy by using Dependency Injection concept. In this article, we will some important differences between Struts 1 and Struts 2 framework.
Below is my list of some common difference between Struts 1 and Struts 2 framework. Let us see the component and functional differences between struts 1.x and struts 2.x.
- In Struts 1, ActionServlet is considered as FrontController while in Struts 2 its Filter, which can be considered as front end controller.
- A useful enhancement in Struts2 is Interceptor API, which allows to do lot of stuff much easily e.g. file upload using Struts2's builtin FileUploadInterceptor class.
- In struts 1.x, we have multiple tag libraries like html, logic, bean etc while in Struts 2.x, we do not have multiple libraries, instead we have single library which includes all tags.
- In Struts 1.x, we used to configure Struts using struts-config.xml, but with Struts 2.x, you can use multiple configuration file, most commonly used as struts.xml.
- In Struts 1 it's mandatory to extend org.apache.struts.action.Action and implement execute() method which returns ActionForward and accept HttpServletRequest and HttpServletResponse. This is not the case with Struts 2, here Action class can be a simple POJO or Java object with execute() method. Also execute() method returns String rather than returning ActionForward object. You can still use ActionSupport class or Action interface but those are completely optional.
- In Struts 2.x, we have annotations support along with programmatic and declarative validations.
- In struts 1.x, declarative validations are done by using validation frame work. While in Struts 2.x, declarative validations are done by using xwork2 framework by webwork the reason being, its support valuations through Annotations.
- In struts 1.x, an Action class is a singleton class, so Action class object is not a thread safe, as a programmer we need to make it as thread safe by applying synchronization. In Struts 2.x, an Action class object will be created for each request, so it is by default thread safe, so we no need to take care about safety issues here.
- In struts 1.x we have only jsp as a view technology while in Struts 2.x, we have support of multiple view technologies like velocity, Freemarker, jasper reports, jsp etc.
- Struts 1 Action classe is dependent on Servlet API in form of HttpServletRequest and HttpServletResponse classes required in execute() method. Struts 2 don't have such dependency and its execute() method doesn't required Servlet API.