Struts Tutorial – Struts 2 Baby Step to Learn

Struts Tutorial

Hi in this struts tutorial we will discuss about struts 2 tutorial covers all the topics of Struts 2 Framework with simplified examples. The struts framework was initially created by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001. The current stable release of Struts is struts-2.3.15 in GA / June 3, 2013.

Struts Tutorial

Lets’s see struts tutorial and below explained the topics.

Struts 2 Framework

Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time. Apache Struts 2 was originally known as WebWork 2.

The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. Struts 2 is the combination of webwork framework of opensymphony and struts1.

struts2 = webwork + struts1

The Struts 2 provides supports to POJO based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc. Struts2 is based on the Web works framework of OpenSymphony.

Struts 2 Features 

The strut-2 framework is designed for the compilation of the entire development cycle including of building, developing and maintaining the whole application. It is very extensible as each class of the framework is based on an Interface and all the base classes are given an extra application and even you can add your own. The basic platform requirements are Servlet API 2.4, JSP API 2.0 and Java 5.

  • Configurable MVC components In struts 2 framework, we provide all the components (view components and action) information in struts.xml file. If we need to change any information, we can simply change it in the xml file.
  • POJO based actions In struts 2, action class is POJO (Plain Old Java Object) i.e. a simple java class.
  • AJAX support Struts 2 provides support to ajax technology. So a part of the page will be changed only and page will not reload. So it makes the performace fast.
  • Integration Support We can simply integrate the struts 2 application with hibernate, spring, tiles etc. frameworks.
  • Various Result Types We can use JSP, freemarker, velocity etc. technology as the result in struts2.

About Struts 2 Framework

  • Struts 2 is based on the OpenSymphony Web Works Framework.
  • Struts 2 framework implements the Model-View-Controller (MVC) design pattern.
  • In Struts 2 the model, view and controller are implemented by the action, result and FilterDispatcher respectively.
  • The controller‘s job is to map the user request to appropriate action.
  • In Struts 2 FilterDispatcher does the job of Controller.
  • Model contains the data and the business logic.
  • In Struts 2 the model is implemented by the Action component.
  • View is the presentation component of the MVC Pattern.
  • In Struts 2 the view is commonly implemented using JSP, Velocity Template, Freemaker or some other presentation-layer technology.

About Struts 2 Request Flow

  1. The controller receives the user request and determine which Struts 2 action to invoke.
  2. The framework creates an instance of this action and associate it with the newly created instance of the ActionInvocation.
  3. In Struts 2 the invocation of action should pass through a series of interceptors as defined in the application’s XML file.
  4. The framework calls the ActionInvocations invoke() method to start the execution of the action.
  5. Each time the invoke() method is called, ActionInvocation consults its state and executes whichever interceptor comes next.
  6. ActionInvocation hands control over to the interceptor in the stack by calling the interceptors intercept() method.
  7. The intercept() method of the interceptor intern calls the invoke() method of the ActionInvocation till all the interceptors are invoked, in the end the action itself will be called and the corresponding result will be returned back to the user.
  8. Some interceptor do work before the action is executed and some do work after the action is executed. It’s not necessary that it should do something each time it is invoked.
  9. These interceptors are invoke both before and after the action.
  10. First all the interceptors are executed in the order they are defined in the stack.
  11. Then the action is invoked and the result is generated.
  12. Again all the interceptors present in the stack are invoked in the reverse order.
  13. The other important features of Struts 2 are OGNL and ValueStack.
  14. Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack.
  15. OGNL help in data transfer and type conversion.
  16. OGNL expression language provides simplified syntax to reference java objects.
  17. OGNL is used to bind the java-side data properties to the string-based view layer.
Struts 2 Tutorial

Architecture of Struts 2 Framework

  • In Struts 2 the action resides on the ValueStack which is a part of the ActionContext. ActionContext is a global storage area that holds all the data associated with the processing of a request.
  • When a request comes the params interceptor helps in moving the request data to the ValueStack.
  • Now the OGNL does the job of converting the string based form data to their corresponding java types. OGNL does this by using the set of available built-in type converters.
  • Again when the results are generated the OGNL converts the java types of the property on the ValueStack to the string-based HTML output.
  • ActionContext is thread local which means that the values stored in the ActionContext are unique per thread, this makes the Struts 2 actions thread safe.
Struts ValueStack

Struts Tutorial Contents

Struts Tutorial : 2 Basic
1 – Introduction To MVC
2 – Model 1 and Model 2(MVC) 
3 – Introduction to Struts 2
4 – Architecture of Struts 2
5 – Setting Up Struts 2 in Eclipse
6 – Writing a Struts 2 Hello World Application
7 – Struts 2 OGNL, ActionInvocation, ActionContex
8 – Configuration of Struts 2
9 – Understanding Namespaces
10 – A Tag And A Business Service
11 – The ValueStack in Struts 2
12 – Struts 2 Actions
13 – Struts 2 Interceptors
14 – Struts 2 Result Types
15 – Struts 2 File Uploads
16 – Struts 2 Database Access
17 – Struts 2 Sending Mails
18 – Struts 2 Validations
19 – Struts 2 Localization
20 – Struts 2 Themes/Templates 
21 – Struts 2 Exception Handling
22 – Struts 2 Annotations
Struts Tutorial : Struts 2 Tags
1 – Struts 2 Control Tags
2 – Struts 2 Data Tags
3 – Struts 2 Form Tags
4 – Struts 2 Ajax Tags
Struts Tutorial : Struts 2 Integrations
1 – Struts 2 with Spring 3 Integration
2 – Struts 2 with Tiles 2 Spring 3 Integration
3 – Struts 2 with Hibernate 3 Spring3 Tiles 2 Integration
4 – Struts 2 And JSON Example

Struts Tutorial References

Struts 2 – Welcome

Struts 2 -Wiki 

Next

2 Comments

  1. Anonymous October 8, 2013
  2. Dinesh October 16, 2013