Explain Struts navigation flow

Editorial / Best Answer

ravi_1229  

  • Member Since Nov-2006 | May 9th, 2009


1) When first the request is made from a JSP/HTML/XSLT to the server with a particular URI(/something.do), the controll first reaches Web.xml file.

2) it checks the mapping for /something.do in web.xml and finds the ActionServlet and loads ActionServlet.
......
<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.
(Note: ActionServlet extends HttpServlet only)
......................
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
........................

4) in init() it takes the init-parameters as struts-config.xml and loads the xml file.

5) then the ActionServlet calls the proceed() of RequestProcessor class.

6) in proceed() , it checks the appropriate mapping for current request, creates and ActionMapping object and maps the URI to the action in the struts-config.xml.

7) then it creates an object to ActionForm associated to that action mapped object

8) calls the setters and reset().

9) if the action tag in struts-config.xml contains validate="true", then the validate method is called.

10) if any validation errors, it return to view component (any jsp,XSLT) which is specified as an attribute of input="/error.jsp"

11) if there are not validation errors, it then creates an Action class for that mapping and search for execute method and executes it.

12) execute method performs the bussinesslogic which you provide and returns with an ActionForward key.

13) now the returned key is searched in the mapping object which is specified as forward tag.

14) it looks for the appropriate view component and performs the getters on that view component and returns the response to the browser.

Simple, Thats it.

Thanks
Ravi

Showing Answers 1 - 8 of 8 Answers

Madhu

  • Mar 23rd, 2005
 

A client requests a path that matches the Action URI pattern.The container passes the request to the ActionServlet.If this is a modular application, the ActionServlet selects the appropriate module.The ActionServlet looks up the mapping for the path.If the mapping specifies a form bean, the ActionServlet sees if there is one already or creates one.If a form bean is in play, the ActionServlet resets and populates it from the HTTP request.If the mapping has the validate property set to true, it calls validate on the form bean.If it fails, the servlet forwards to the path specified by the input property and this control flow ends.If the mapping specifies an Action type, it is reused if it already exists or instantiated.The Action?s perform or execute method is called and passed the instantiated form bean (or null).The Action may populate the form bean, call business objects, and do whatever else is needed. The Action returns an ActionForward to the ActionServlet.If the ActionForward is to another Action URI, we begin again; otherwise, it?s off to a display page or some other resource. Most often, it is a JSP, in which case Jasper, or the equivalent (not Struts), renders the page.

  Was this answer useful?  Yes

pratyusha

  • Sep 4th, 2005
 

Flow:: 
1) Action servlet gets the request.It initiates the servlet-config.xml and selects an appropriate page as per the mapping 
2)If the accessed file is .java it does the appropriate action and fwd it to the file specified in the strus-config.xml. If the accessed path is .jsp it invokes the action bean where in the validation for the user input is done,if it fails errors are displayed..if it succeeds the action servlet is kicked and does the steps in execute() method..and fwd it to the necessary page as in mappings.foward()

  Was this answer useful?  Yes

Rohit Ankushe

  • Oct 13th, 2005
 

The Struts Flow is like this the very first request  comes to the Action servlet which the part of the Controller component of the MVC architeture , then the requets is disptachted to the request processor the request processor find the specified mapping url of the response in invoke the page.

The struts config.xml  use to define as the mapping from the pages.After finding the respective mapping the bean is populated if its their and the forward to the requested path.

  Was this answer useful?  Yes

Actual flow

The struts framework are most flaxible and example of loose coupling component

1-web.xml->actionservlet

2-Actionservlet class call the RequestProcesser class which deside the appropriate action for perticular request.

3-struts-config

4-formbean & action class

5-perticular jsp

  Was this answer useful?  Yes

satheesh kumar. sriramaneni

  • May 4th, 2006
 

When we deploy our application in the server, at first the container reads the information from web.xml file.Here ActionServlet object will be created and init() of ActionServlet will be called.Here ActionServlet is the backbone to the whole application.

When client send a rewuest using .jsp extension , getters() and reset() of FormBean will be called. When client fill the form and press on submit button, then setters() and validate() will be called.
If the data is not valid ,then the form redirects to another page which is specified in struts-config.xml file. If the data is valid , then only Action class object will be created.

In Action class , have execute() which have return type of ActionForward. We can specify the business logic in Model and provide that object in execute().
After completion of business logic execution , then it forwards to another page ( either success or failure) , whichis specified in struts-config.xml file.

ravi_1229

  • May 8th, 2009
 

1) When first the request is made from a JSP/HTML/XSLT to the server with a particular URI(/something.do), the controll first reaches Web.xml file.

2) it checks the mapping for /something.do in web.xml and finds the ActionServlet and loads ActionServlet.
......
<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.
(Note: ActionServlet extends HttpServlet only)
......................
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
........................

4) in init() it takes the init-parameters as struts-config.xml and loads the xml file.

5) then the ActionServlet calls the proceed() of RequestProcessor class.

6) in proceed() , it checks the appropriate mapping for current request, creates and ActionMapping object and maps the URI to the action in the struts-config.xml.

7) then it creates an object to ActionForm associated to that action mapped object

8) calls the setters and reset().

9) if the action tag in struts-config.xml contains validate="true", then the validate method is called.

10) if any validation errors, it return to view component (any jsp,XSLT) which is specified as an attribute of input="/error.jsp"

11) if there are not validation errors, it then creates an Action class for that mapping and search for execute method and executes it.

12) execute method performs the bussinesslogic which you provide and returns with an ActionForward key.

13) now the returned key is searched in the mapping object which is specified as forward tag.

14) it looks for the appropriate view component and performs the getters on that view component and returns the response to the browser.

Simple, Thats it.

Thanks
Ravi

devabscmca

  • Oct 6th, 2010
 

Struts flow

After developing the struts application after deploying it is the server & when you started the server then following things will happened

Container initializes the web.xml

When container reading the data from web.xml container checks all the servlets configure if container at the load time then container try to load the servlet in to the main memory in our same are they configure one servlet called action servlet with load on startup tag

Because of the container loads the action servlet in to the memory,instance will be created and then init method will be called.

Inside the init method they implement the code using SAX parser to read the data from struts configuration time(to file which you are passing init parameter) once init method is completed all the data from struts configuration file will be loaded into main memory.if any problem happened while passing the xml documents error will be located the file and may not allow tomcat startup.

When you send the request to post problem.jsp to display that in the browser following things will happened :---------

1st request will be given to action servlet

Action servlet receive the request and then delegate the request to request processor.

Upon receiving the request by calling the following things

Incoming request uri(from tag action attribute value) will be compare against path attribute value of action tags configure in action mapping .if the mapping action is not found.

Then the error message (500) called “cannot retrieve mapping for the action/post99”it will be display to client.

If the matching action is found then its name attribute value will be taken

With that name RP (request parameter) verify all the form bean configured to check any form bean tag name is mapping with if no names is matching with the given name then error message called “cannot retrieve definition for from bean Form will be display to the client. If it is found then its types attribute value will be taken which is nothing but form bean java class fully qualified name.

Form bean java class will be verified if it is not found then error message called exception creating bean of class com.jlc .dev problem from 99 which will you not provided.

If the class is found then it will be loaded and it will be instanciated and initialized.

Form bean object will be stored in the given scope (value of scope of scope attribute)

The requested JSP’s display to the client (Post problem.jsp)   

When user check the submit button after entering in the fields then following things will happened :-----------------

Request will be submitted to action servlet

Action servlet delegates the request to request processor.

Request processors identify the corresponding action for the incoming request.

RP identify the form bean name and then its type.

RP retries as creates the form bean object.

Populate the clients submitted into form bean object.

RP takes the types of the action (avction belongs name will be..)

Action class within form instantiated in if r is coming at the 1st time otherwise created instance will be retrives from application scope RP invokes the executed by pass the  following  four parameter :---------------

Action mapping

Action form

HttpServletRequest

HttpServletResponse

Code inside the execute method interact with the model component and get the request after passing the results action forward object will be returned by the executed method to request processor.

RP takes the returned action forward object(which is associated with forward name) and identify the corresponding forward in struts configuration files

RP takes the path(jsp) specified for the identified forward and the same will be forwarded to the client 

  Was this answer useful?  Yes

Struts navigation flow
================

1) When application server gets started,container loads the web.xml

1) When first request is made from a JSP/HTML/XSLT to the server with a particular URI(/something.do) the control first reaches Web.xml file.

2) it checks the mapping for /something.do in web.xml and finds the ActionServlet and loads ActionServlet.
......
<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.
(Note: ActionServlet extends HttpServlet only)
......................
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
........................

4) in init() it takes the init-parameters as struts-config.xml and loads the struts config xml file.

5) then the ActionServlet calls the process() method of RequestProcessor class.

6) process() method checks the appropriate action inside the action mapping for current request and maps the request URI to the corresponding action in the
struts-config.xml.

7) Then it creates an object to ActionForm associated to that action mapped object

8) Then it calls the setters and reset() methods of this form bean(which extends ActionForm)

9) If the action tag in struts-config.xml contains validate "true" then the validate method is called.

10) if any validation errors it return to view component (any jsp XSLT) which is specified as an attribute of input "/error.jsp"

11) if there are no validation errors it then creates an Action class for that mapping and search for execute method and executes it.

12) execute method performs the bussinesslogic which you provide and returns with an ActionForward key.

13) Now the returned key is searched in the mapping object which is specified as forward tag.

14) It looks for the appropriate view component and performs the getters on that action form corresponding to this view component and loads the view page in the browser.


- Prabhakar

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions