Home » Struts » Struts Flow-How Struts Works

Struts Flow-How Struts Works

A typical struts web.xml file is mentioned below.


<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

After developing and deploying Struts application on the server, when you start the server following things will happen.

Container initializes(read the data) web.xml file.When container is reading the data from web.xml It will check all the Servlets configured. If container contains <load-on-startup> tag then it will try to load the Servlet into main memory .In our case we configured only one Servlet called ActionServlet with <load-on-startup> tag, because of this container loads the ActionServlet into the memory.Servlet container Instantiate the ActionServlet and then init() method will be called.

Inside the init() method there is logic using SAX parser to read the data from struts configuration file.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/reported to log file and may not allow tomcat startup.

If the user types http://localhost:8080/app/submitForm.do in the browser URL bar, the URL will be intercepted and processed by the ActionServlet since the URL has a pattern *.do, with a suffix of "do". Because servlet-mapping is


<servlet-mapping> 
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

Then ActionServlet delegates the request handling to another class called RequestProcessor by invoking its process() method.

The RequestProcessor does the following in its process() method:

ActionMapping from struts-config.xml


<action-mappings>
<action path="/submitForm"
type="com.example.EmpAction"
name="EmpForm"
scope="request"
validate="true"
input="myform.jsp">
<forward name="success"
path="success.jsp"/>
<forward name="failure" path="failure.jsp" />
</action>
</action-mappings>
<form-beans>
<form-bean name="EmpForm" type="com.example.form.LoginForm"/>
</form-beans>

The RequestProcessor looks up the configuration file for the URL pattern /submitForm (if the URL is http://localhost:8080/app/submitForm.do).

If the mapping action is not found,then the error message will be displayed to the client.

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

With that name RP (request processor) verifies all the form-bean configured to check any form bean tag name is mapping with that name. If no names is matching with the given name then error message called "cannot retrieve definition for from bean LoginForm" will be displayed to the client.

If it(Form-Bean definition) is found then its "type" 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 will be reported.

If the class is found then RequestProcessor instantiates that class and puts it in appropriate scope - either Session or Request.The RequestProcessor determines the appropriate scope by looking at the scope attribute in the same ActionMapping.

RequestProcessor iterates through the HTTP request parameters and populates the FormBean.

The RequestProcessor checks for the "validate" attribute in the ActionMapping. If the validate is set to true, the RequestProcessor invokes the validate() method on the EmpForm instance. This is the method where you can put all the html form data validations.

If Validate fail the RequestProcessor looks for the input attribute and return to JSP page mentioned in input tag. If Validate pass goto next step.

The RequestProcessor instantiates the Action class specified in the ActionMapping (EmpAction) and invokes the execute() method on the EmpAction instance.


Signature of the execute method is:

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
//your logic
return mapping.findForward("success");
}

In return mapping.findForward("success") RequestProcessor looks for the success attribute and forward to JSP page mentioned in success tag. i.e success.jsp.
In return mapping.findForward("failure") RequestProcessor looks for the failure attribute and forward to JSP page mentioned in failure tag. i.e. failure.jsp


Previous Next Article

comments powered by Disqus