Powered by Blogger.

Struts Flow and Architecture

>> Saturday, June 6, 2009

Before going to know about Struts Flow and Architecture we get to know What is Struts Frame work? As per simple definition "Struts is an open Source framework, given by Apache Software Foundation, which is used develop MVC2 based web applications in java". Struts contains Validations, I18N(Internationalization and Exception handling features. Because of these three Struts became very popular.

Web.xml entries will be read by WebContainer of WebServer or Application Server software. Struts configuration file entry is will be read by ActionServlet.

WebContainer creates Actionservlet object where as FormBean class object, ActionClass object will be created by ActionServlet.
Struts Flow and Architecture_001

  •  In the above diagram Business Logic is kept in Action class ..so it is called as "MODEL".
  •  Execute method in Action class is fixed method.Earlier this method is given as perform(mapping,form,request,response)method
  • Perform(mapping,form,request,response) is the deprecated method of execute(mapping,form,request,response) method form Struts1.1 version onwards.
  •  Struts 1.0-> perform(mapping,form,request,response) method
  • Struts 1.2,1.3 -> perform(mapping,form,request,response) (deprecated) , execute(mapping,form,request,response).
With respect to the above diagram:

1. End user submits the request from JSP form(register.jsp) to struts based web application.
2. Based on the configuration done in web.xml ActionServlet traps the request and takes the result.
3. ActionServlet uses struts configuration file entries to decide FormBean, Action class to be utilized to process the request.
4. ActionServlet creates or locates FormBean class object and writes received form data to FormBean class object.
5. ActionServlet creates or locates Action class object.
6. ActionServlet calls execute(mapping,form,request,response)method of Action class, Business logic of execute(mapping,form,request,response) method executes and result will be generated.
7. ActionServlet takes the control from Action Class.
8. ActionServlet uses Action forward configurations of struts configuration file to decide result JSP(result.jsp) page of Action Class.
9. ActionServlet forwards the control to result page called result.jsp.
10. Presentation logic of result page executes(result.jsp) to format result goes to browser as response.
11. This formatted result goes to browser as response.

//Reading struts Configuration file.

Reading request data, creating or locating FormBean, Action class Objects are the responsibilities of ActionServlet.

X.jsp ----> Form
XForm ---> FormBean class
XAction ---> Action class

register.jsp ---> Form
Register form ---> FormBean
Register Action ---> Action class

naming conventions are very useful to make sources of the application self-descriptive.

//Any .xml can act as Structs Configuration file. there is no default struts configuration file name.
  • If Business Logic is kept in Action class then it is called MODEL.
  • If Action Class contains logic to interact with other model components like EJB, SPRING WITH HIBERNATE applications then Action Class is called helper to controller Servlet called ActionServlet.
Q: Which roll you prefer for ActionClass.
A: We always to recommended not to place business logic in action class.
Reasons are:
  • Business logic becomes specific to one Web Application(Business logic is not reusable).
  • Allows only HTTP clients to access the business logic. AWT application, SWING applications,Standalone Java applications are called "nonHttp applicaions".
  • Middleware services(additional services) must be implemented manually to overcome all these problems keep business logic in EJB components are spring with Hibernate applications and make a Action class helper to controller to communicate with these model components.
Note: In most of the projects action class will be taken as helper to Controller.

Go through this Basic Struts Login Application. This will help you to understand the flow easily.

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.