Understanding the Role of Struts Action Class in Web Applications, Lecture notes of Java Programming

The role and functionality of the struts action class in web applications. It acts as an interface between the view and model layers, transferring data and invoking business logic. The document also covers the process of creating and using an action class with an example.

Typology: Lecture notes

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Understanding Struts Action Class
An 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 interface 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 processed 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)
selects 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 db layer.
The ActionServlet (command) 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:
Tes t Ac ti on .j av a
package SturtsTest02;
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 Tes tAc ti on extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
Comment [1]: start source code
docsity.com
pf2

Partial preview of the text

Download Understanding the Role of Struts Action Class in Web Applications and more Lecture notes Java Programming in PDF only on Docsity!

Understanding Struts Action Class

An 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 interface 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 processed 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) selects 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 db layer.

The ActionServlet (command) 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 SturtsTest02;

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,

Comment [1]: start source code

docsity.com

HttpServletResponse response) throws Exception{ return mapping.findForward("testAction"); } }

docsity.com