JavaBeat
Struts Tutorials | Hibernate Tutorials | JSP Tutorials | Servlet Tutorials | EJB Tutorials | Struts Resources | Spring Resources | Hibernate Resources
Struts Home Articles Resources Tutorials FAQs Forums

1. Introduction to the Apache Struts

2. Introduction to the Struts Controller

3. Introduction to the Struts Action Class

4. Using Struts ActionFrom Class

5. Using Struts HTML Tags

6. Introduction to Struts Validator Framework

7. Client Side Address Validation in Struts

8. Developing Application with Struts Tiles

9. Using tiles-defs.xml in Tiles Application

10. Struts DynaActionForm

11. Struts File Upload

In this lesson I will show you how to use Struts Action Class and forward a jsp file through it. 


What is Action Class?


The Action Class is part of the Model and is a wrapper around the business logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic. To use the Action, we need to  Subclass and overwrite the execute() method. In the Action Class all the database/business processing are done. It is advisable to perform all the database related stuffs in the Action Class. 

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)




Parameters:

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

Adding the Action Mapping in the struts-config.xml
To test the application we will add a link in the index.jsp 

<html:link page="/TestAction.do">Test the Action</html:link>

Following code under the <action-mappings> tag is used to for mapping the TestAction class.


<action
path="/TestAction"
type="roseindia.net.TestAction">
<forward name="testAction" path="/pages/TestAction.jsp"/>
</action>

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.


Parameters:

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

Adding the Action Mapping in the struts-config.xml
To test the application we will add a link in the index.jsp 

<html:link page="/TestAction.do">Test the Action</html:link>

Following code under the <action-mappings> tag is used to for mapping the TestAction class.


<action
path="/TestAction"
type="roseindia.net.TestAction">
<forward name="testAction" path="/pages/TestAction.jsp"/>

To test the new application click on Test the Action
link on the index page. The content of TestAction.jsp should be displayed on
the user browser.




In this lesson you learned how to create Action Class
and add the mappings in the struts-config.xml. Our Action Class returns the ActionForward
mapping of the TestAction.jsp.




Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
Describe the lifecycle of a receiver application in order to receive a message?
Messages are not successful until they have been acknowledged. What are the types of acknowledgments?
What happens to messages if a transaction is rolled back?
What is the Role of the JMS Provider?
What is JMS administered object ?

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat