Monday, September 11, 2017

What is Struts 2 and how it works

Image credits:

 Apache Struts framework is a free, open-source, MVC framework for creating elegant, modern Java web applications. It favors convention over configuration, is extensible using a plugin architecture, and ships with plugins to support REST, AJAX and JSON.

Apache Struts2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.
Struts 2 = Webwork + Struts 1 

Some of the framework's best features

  • Pluggable framework architecture that allows request lifecycles to be customized for each action.
  • Flexible validation framework that allows validation rules to be decoupled from action code.
  • Hierarchical approach to internationalization that simplifies localizing applications.
  • Automatic type conversion that transparently maps values from HTTP to native Java data objects, solving one of the most tedious efforts when creating web applications.
  • Integrated dependency injection engine that manages component lifecycle and dependencies.
  • Modular configuration files that use packages and namespaces to simplify managing large projects with hundreds of actions.
  • Java 5 annotations that reduce configuration overhead. (Java 1.4 is the minimum platform.)

  • Reusable user interface tags that allow for easy component-oriented development using themes and templates. Bundled tags ranges from simple text fields to advanced tags like date pickers and tree views.
  • JSTL-compatible expression language (OGNL) that exposes properties on multiple objects as if they were a single JavaBean.
  • Pluggable Result Types that support multiple view technologies, including JSP, FreeMarker, Velocity, PDF, and JasperReports.
  • Optional AJAX theme that simplifes creating interactive web applications.
  • Optional Interceptor plugins that can execute long-running queries in the background, prevent multiple form submissions, or handle custom security schemes.

  • Easy integration with other popular products, including Hibernate, Spring, SiteMesh, and JSTL.
  • Distributed under the business-friendly Apache License 2.0.

Architecture and Request Processing Lifecycle
Struts 2 Architecture is based on WebWork 2 framework. It leverages the standard JEE technologies such as Java Filters, JavaBeans, ResourceBundles, Locales, XML etc in its architecture.

Image credits:

  1. The normal lifecycle of struts begins when the request is sent from client. This results invoke the servlet container which in turn is passed through standard filter chain.
  2. The FilterDispatcher filter is called which consults the ActionMapper to determine whether an Action should be invoked.
  3. If ActionMapper finds an Action to be invoked, the FilterDispatcher delegates control to ActionProxy.
  4. ActionProxy reads the configuration file such as struts.xml. ActionProxy creates an instance of ActionInvocation class and delegates the control.
  5. ActionInvocation is responsible for command pattern implementation. It invokes the Interceptors one by one (if required) and then invoke the Action.
  6. Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.
  7. The Interceptors are executed again in reverse order and the response is returned to the Filter (In most cases to FilterDispatcher). And the result is then sent to the servlet container which in turns send it back to client.

No comments:

Post a Comment