Pages

Tuesday, January 3, 2012

Struts Flow


Steps 1. 
ActionServlet 
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 it appropriately. 

In the web.xml 

 
action 
org.apache.struts.action.ActionServlet 
 
 
config 
/WEB-INF/struts-config.xml 
 
1 
 

 
action 
*.do 
 

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 
*.do. Any *.do will call ActionServlet class. 
ActionServlet calls the process() method of RequestProcessor class 

Step 2. 
ActionServlet calls the process() method of RequestProcessor class. 
The RequestProcessor first retrieves appropriate XML block for 
the URL from struts-config.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 

 

type="mybank.example.CustomerAction" 
name="CustomerForm" 
scope="request" 
validate="true" 
input="CustomerDetailForm.jsp"> 

path="ThankYou.jsp" 
redirect=?true?/> 
 
 

Step 3. 

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. 

Step 4. 
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. 

Step 5. 
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. 

Step 6. 
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 
RequestProcessor 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. 

Step 7. 

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.

No comments:

Post a Comment