Skip to main content

Struts Flow

Hi All,

    Here are the simple 10 steps for how Struts Framework will work - It is a general question in many Interviews!

    Initially let us assume /login.do is our action - the result will be successful login.  Here I will explain 15 steps how/what Struts framework will do ?


  1. As end user triggerred / clicked on /login.do - > as usual it is a web application - first it will look for Web.xml So here - Web.xml loading is the first step!
  2. In Web.xml it looks for *.do mapping and it finds the Struts plug-in  i.e., ActionServelt and it loads the same.
    Example of Mapping:
       <servlet-name>action</servlet-name>
       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
       .....
       <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
       </servlet-mapping>
  3. As part of loading ActionServlet calls the init() as every other servlet does.
     <init-param>
     <param-name>config</param-name>
     <param-value>/WEB-INF/struts-config.xml</param-value>
     </init-param>
    So it loads the struts-config.xml - Same here you can configure multiple strtus config xml files
    Note : ActionServlet extends HttpServlet only
  4. Then the ActionServlet calls the process() method of RequestProcessor class.
  5. process() method checks for the appropriate action in the action mapping for current request and maps the URI to the action in the struts-config.xml.  Then it creates an object to ActionForm associated to that action.
  6. It then calls the setters and reset() methods in the form bean or POJO or Java Bean class (which extends ActionForm) corresponds to the mapped action. (Or else you can avoid this class by writing DynaActionForm - i.e. you can create beans in config xml itself) 
  7. If the action tag in struts-config.xml contains validate "true" then the validate method will call. So that you can check for normal validations like string length or mismatch of strings/ passwords etc. If any validation messages exist - add them to MessageCollection and iterate them in your front end jsp page. If not it will redirect to the Action class - to perform actual Business Logic. 
  8. Inside Action class - execute() method performs the business logic by calling internal methods/DAO calls (you can use Hibernate whatever - as this is not related to Struts concept) which you provide and based on these method calls it returns with an ActionForward string key. (of course you have to set the result in session.setAttribute to get these result in resulting jsp) 
  9. Now the returned string key will search for corresponding forward jsp (Inside <action> tag using <forward> tag you can mention the forward names) 
  10. It looks for the appropriate view component and performs the getter on that action form corresponding to this view component and loads the view page in the browser. (Iterate the result from session.getAttribute which you have set while you returned from the DAO calls)!
Here is the Sample Action Tags for your reference! 

   <action path="/loginEntry" type="com.sharath.frontend.action.LoginAction"
           name="loginForm" parameter="method" validate="true">
       <forward name="success" path="mainEntryPage" />
       <forward name="loginEntry" path="loginEntryPoint" />
       <forward name="registerEntry" path="registerEntryPoint" />
   </action>

    <form-bean name="loginForm" type="com.sharath.frontend.LoginForm" />


More concepts on Struts soon! Keep Visiting!


Please share this - Because Sharing is Gaining in Knowledge!

Comments

Popular posts from this blog

OOPS concepts Tutorial

OOPS concepts Tutorial Summary: By the end of this tutorial "OOPS concepts Tutorial", you will understand the meaning of OOPS concepts. Three basic concepts of OOPS Abstraction Encapsulation Polymorphism 1. Abstraction     Abstraction means using the equipment (or code) without knowing the details of working. For example, you are using your mobile phone without knowing how a mobile operates internally. Just a click on a button connects to your friend. This is abstraction. That is, the details of mobile mechanism are abstracted. Similarly we use the code of somebody to get the functionality without knowing how the code is written or working. For example, you are using printf() function to write to the DOS prompt without the awareness of what the code of printf(). One of the ways of achieving abstraction is inheritance. Through inheritance a subclass can use the super class methods as if they belong to it without caring their implementation details. 2. Encapsula...

Basic Definitions

Basic Definitions Class :     A class is a blueprint or prototype from which objects are created.                                                            (or) A Class is a template or Blue print is used to bind or group the related data members along with its related functionalities                                                            (or)     A class is a prototype that defines the variables and the methods common to all objects of a certain kind. Member Functions operate upon the member variables of the class. Object :  Objects is instance of a class it contains state and behaviors, State is nothing but variables and beha...
Struts Actions Forward Action :          In Struts MVC model, you have to go through the Action Controller to get a new view page. In some cases, you really just need to get a specified JSP page only, it's so stupid to create an action controller class which just forward the pate to you.        ForwardAction acts as bridge from the current view and pages it link to.ForwardAction uses the RequestDispatcher to forward to a specified web resource.This allow you to link to an action instead of directly to a jsp.  Used of Forward Action :- The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions.  ForwardAction used to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page. Include Action :         Includ...