Example & Tutorial understanding programming in easy ways.

What\'s the role of Action Class in Struts?

An Action class in the struts application is a POJO based class which extends Struts ''org.apache.struts.action.Action" Class. 
Action class acts as wrapper around the business logic and provides an interface to the application's Model layer.
 
An Action class works as an adapter between the contents of an incoming HTTP request and the business logic that corresponds to it. 
Then the struts controller (ActionServlet) selects an appropriate Action and Request Processor creates an instance if necessary, 
and finally calls execute method of Action class. 

To use the Action class, we need to Subclass and overwrite the execute() method. and our business login should be placed in execute() method.
 
The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the JSP as per the value of the returned ActionForward object. 
ActionForward JSP from struts_config.xml file. 

Now, here is an example wich is used developing our Action Class according our request : 

Our Action class (StudentRecord.java) is simple class that only forwards the success.jsp. 
Our Action class returns the ActionForward called "success", which is defined in the struts-config.xml file (action mapping is show later in this page). 
Here is code of our Action Class 

public class StudentRecord extends Action 
public ActionForward execute( 
ActionMapping mapping, 
ActionForm form, 
HttpServletRequest request, 
HttpServletResponse response) throws Exception{ 
return mapping.findForward("success"); 

mapping.findForward("success"); forward to JSP mentioned in struts_config.xml. 
struts_config.xml configuration is : 

<action 
path="/EmpAction" 
type="com.techfaq.EmpAction"> 
<forward name="success" path="/success.jsp"/> 
</action> 
mapping.findForward("success") method forward to success.jsp (mentioned in struts_config.xml); 

Here is the signature of the execute() method Action Class. 

public ActionForward execute(ActionMapping mapping, 
ActionForm form, 
javax.servlet.http.HttpServletRequest request, 
javax.servlet.http.HttpServletResponse response) 
throws java.lang.Exception 

Where , mapping - The ActionMapping used to select this instance 
form - The optional ActionForm bean for this request (if any) 
request - The HTTP request we are processing 
response - The HTTP response we are creating 
Throws: 
Action class throws java.lang.Exception - if the application business logic throws an exception 

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!