Flow Control in Struts
The Flow control in Struts are explained below
The central component of the Struts Controller is the ActionServlet. It is a concrete
class and extends the javax.servlet.HttpServlet. It performs two important things. On
startup, its reads the Struts Configuration file and loads it into memory in the init()
method. In the doGet() and doPost() methods, it intercepts HTTP request and handles
In the web.xml
If the user types http://localhost:8080/App1/submitDetails.do in the browser URL
bar. Server will call ActionServlet class because in the url-pattern the mapping is
Any *.do will call ActionServlet class. ActionServlet calls
the process() method of RequestProcessor class
ActionServlet calls the process() method of RequestProcessor class. The
RequestProcessor first retrieves appropriate XML block for the URL from strutsconfig. xml. This XML block is referred to as ActionMapping in Struts terminology. In fact there is a class called ActionMapping in org.apache.struts.action package. ActionMapping is the class that does what its name says it holds the mapping between a URL and Action.
A sample ActionMapping from struts-config.xml
<forward name="failure" path="error.jsp" />
The RequestProcessor looks up the configuration file for the URL
pattern /submitDetails. and finds the XML block (ActionMapping) shown above. The type attribute tells Struts which Action class has to be instantiated.
The RequestProcessor instantiates the CustomerForm 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.
Next, RequestProcessor iterates through the HTTP request parameters and populates the CustomerForm properties of the same name as the HTTP request parameters using Java Introspection.
Next, 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 CustomerForm instance. This is the method where you can put all the html form data validations. If any error then equestProcessor checks for the input attribute in the ActionMapping and forward to page mentioned in the input tag. If no error in validate ()
method then continue.
The RequestProcessor instantiates the Action class specified in the ActionMapping (CustomerAction) and invokes the execute() method on the CustomerAction instance. The signature of the execute method is as follows.
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
The execute() method returns ActionForward.ActionForward forward = mapping.findForward(“success”);
return forward. will forward to ThankYou.jsp.
ActionForward forward = mapping.findForward(failure);
return forward. will forward to error.jsp.