1
Answer

What are action and action form classes in Struts?

Photo of Abhishek Dubey

Abhishek Dubey

13y
1.8k
1
Hi
   We want to action and action form classes in Struts?

Answers (1)

0
Photo of Satyapriya Nayak
NA 39.3k 13.3m 13y
Hi Abhishek,


Action class in the struts application extends Struts 'org.apache.struts.action.Action" Class. Action class acts as wrapper around the business logic and provides an inteface to the application's Model layer. It acts as glue between the View and Model layer. It also transfers the data from the view layer to the specific business process layer and finally returns the procssed data from business layer to the view layer.

An Action 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) slects an appropriate Action and creates an instance if necessary, and finally calls execute method.

To use the Action, we need to  Subclass and overwrite the execute() method. In the Action Class don't add the business process logic, instead move the database and business process logic to the process or dao layer.

The ActionServlet (commad) passes the parameterized class to Action Form using the execute() method. The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object.

Developing our Action Class?

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

TestAction.java

package roseindia.net;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TestAction extends Action
{
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse responsethrows Exception{
  return mapping.findForward("testAction");
  }
}

 
Understanding Action Class
Here is the signature of the Action Class.

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

Action Class process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic. Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.



Action classes are defined to handle request. Actions exist between Model and view of an application. The struts-config.xml file designates the Action classes that handle requests for various URLs. The struts-config.xml file then decides which JSP page should apply to that situation. Generally Actions do the following:

Invoke business rules
Return Model objects to the View to display.

Actions have a life cycle similar to servlets. They are like servlets and they're multithreaded.

ActionForm class:

An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.

Please refer the below link

http://www.roseindia.net/struts/strutsActionForms.shtml

http://www.roseindia.net/struts/understandingstruts_action_class.shtml

Thanks