Question

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.

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top