Tuesday, May 28, 2013

Struts 1.x Flow - in 10 Simple Steps

Posted by Sharath
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:


3) As part of loading ActionServlet calls the init() as every other servlet does.
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" />

<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!