Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow of control in Struts 1.2 (Lifecycle)

Title may sound a little vague but I'll give it a go. I have 2 servlets:

  1. one.java: Extends the Action class forwards the page to success or failure based on the inputs at index.jsp
  2. two.java: Extends the ActionForm class, Has getters and setters method

I have 3 jsp files:

  1. index.jsp: Is the welcome pages and asks for a username combination
  2. success.jsp: Is called if the combination is correct
  3. failure.jsp: Is called if the combination is false

I have 2 xml files:

  1. web.xml: DD
  2. struts-config.xml: Struts config file

I understand how web.xml works. My only doubt is, which one of the, one.java /two.java is called first from the struts.xml?

I tried to debug and found out that the ActionForm class i.e two.java is called first, then it returns the value to the Actioni.e one.java.

But isn't Action class is supposed to execute first,and then the action form ? I mean This is what MVC architecture follows.

Please explain. Links to a very highly detailed workflow would be really helpful.

like image 368
Punjan Sudhar Avatar asked Oct 22 '13 08:10

Punjan Sudhar


People also ask

What is the flow of struts?

Struts 2 standard flow (Struts 2 architecture)Container maps the request in the web. xml file and gets the class name of controller. Container invokes the controller (StrutsPrepareAndExecuteFilter or FilterDispatcher). Since struts2.

What is the life cycle of action form struts?

The different actions that is performed and the struts flow in the struts life cycle are depicted below. 1) In first step user sends a request to the server for some resource. 2) The filterDispatcher accept the request and then it determines the appropriate action.

What's the flow of request in struts based applications?

Struts Applications utilize the MVC designs, and the flow of demand occurs in a prescribed way: The user associated with the view after clicking on a connection or after that they submit present a form. After the interaction happens, the demand is sent to the controller.

What is the role of controller in struts?

The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations.


1 Answers

It is not surprising that ActionForm class is called before Action - Struts form should be filled with user's data before calling of Struts action method, any of which has 4 parameters:

ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response

Second one - ActionForm - should be ready to allow furthest data processing. I've just found great sequence diagram to illustrate all Struts lifecycle stages:

enter image description here

In short:

  1. After getting client's request Struts front-controller calls RequestProcessor to find out appropriate action and form using struts-config.xml
  2. RequestProcessor gets Struts form object (or creates it if it doesn't exist), populates with data from request, initiates validation (if exists) and calls appropriate Struts action.
  3. Struts action performs all furthers necessary operations.
like image 153
bsiamionau Avatar answered Nov 01 '22 19:11

bsiamionau