What is Struts Action Class in Java J2EE - How to use

What is Action class in Struts
Struts in java is a framework, used to make web application its is based on Model View Controller or MVC design Pattern where Model represent the internal state and action used to change the state view represent presentation component and controller is responsible for receiving the request from client and decide which business logic should be called. Basically Struts have different classes to represent this Model, View and Controller we call them as Action, Action Form, and Action Servlet. So

Model – Action classes
View - Action form classes
Controller – Action Servlet classes

In this article we are focusing on the Model layer of struts framework. Action class is used to provide an interface to application model layer.  What is Action class and how to use Action class is a also a popular Struts interview Question asked in various J2EE interviews.

What is Action Class in Struts ?

What is Action Class in Struts How to useAction Class in Struts framework is used to define the business logic it handle the client request prepare the response or tell where the response should be forward, basically its receive the data from the view layer and forward that data to specific business layer or process the data and again forward the processed data to view layer.
For example Action class can create a library object add books to that library object, it can store selected books by specific reader to session.

In Brief we can describe Action as:

Ø       Our Action class is extended by “org.apache.struts.action.Action class.
Ø       We need to override execute() method of Action class.
Ø       Action servlet select Action class for incoming HTTP Request defined under the action mapping tag in struts config.xml file
Ø       Action class is used to invoke business or data access logic get data from bean and store processed data to bean and return result or errors depending upon the situation,
Ø       Action classes are multi-threaded so we need to carefully handle the action variable as they are not thread safe when working with multi-threaded environment.

How to Use Action Class
In simple steps we will see how to use Action class.
1.       First extend the org.apache.struts.action.Action class.

2.       Override following method 
           public ActionForward execute(ActionMapping mapping,
                    ActionForm form,
                    HttpServletRequest request,
                    HttpServletResponse response) throws IOException, ServletException

Here we develop our business logic and prepare the response to return the client.

  3.Then we configure this action to struts config file of our application inside action mapping :

      <forward name="success" path="/ExamplePages/TestAction.jsp"/>

This is all about the action class in next article will deal with different type of action class provided by  struts framework.

Other Java article from Java67 Blog

No comments:

Post a Comment