Java - Struts material

Q. What is Action Class?
Ans:
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.

Q.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 response) throws 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.
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
Test the Action
Following code under the tag is used to for mapping the TestAction class.
path="/TestAction"
type="roseindia.net.TestAction">


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.
What is ActionForm?
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.
We will first create the class AddressForm which extends the ActionForm class. Here is the code of the class:

AddressForm.java
package roseindia.net;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.struts.action.*;
/**
 * Form bean for the Address Entry Screen.
 *
*/
public class AddressForm extends ActionForm
{
  private String name=null;
  private String address=null;
  private String emailAddress=null;
 
  public void setName(String name){
    this.name=name;
  }
 
  public String getName(){
    return this.name;
  }
 
  public void setAddress(String address){
    this.address=address;
  }
 
  public String getAddress(){
    return this.address;
  }
 
 
  public void setEmailAddress(String emailAddress){
    this.emailAddress=emailAddress;
  }
 
  public String getEmailAddress(){
    return this.emailAddress;
  }
 
 
    /**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    this.name=null;
    this.address=null;
    this.emailAddress=null;
    }
 
    /**
     * Reset all properties to their default values.
     *
     * @param mapping The mapping used to select this instance
     * @param request The servlet request we are processing
   * @return errors
     */
  public ActionErrors validate( 
      ActionMapping mapping, HttpServletRequest request ) {
      ActionErrors errors = new ActionErrors();
 
      if( getName() == null || getName().length() < 1 ) {
        errors.add("name",new ActionMessage("error.name.required"));
      }
      if( getAddress() == null || getAddress().length() < 1 ) {
        errors.add("address",new ActionMessage("error.address.required"));
      }
      if( getEmailAddress() == null || getEmailAddress().length() < 1 ) {
        errors.add("emailaddress",new ActionMessage("error.emailaddress.required"));
      }
 
      return errors;
  }
 
} 
AddressAction.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 AddressAction extends Action
{
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{
      return mapping.findForward("success");
  }
}

Now we have to create an entry for form bean in the struts-config.xml. Add the following lines in the struts-config.xml file:

name="AddressForm"
type="roseindia.net.AddressForm"/>
Add the following line in the struts-config.xml file for handling the action “/Address.do”:
path="/Address"
type="roseindia.net.AddressAction"
name="AddressForm"
scope="request"
validate="true"
input="/pages/Address.jsp">


Now create Address.jsp, which is our form for entering the address details. Code for Address.jsp is as follows:
Address.jsp
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

Please Enter the Following Details
Name
Address
E-mail address
Save Cancel




User enter the values in the form and click on the submit form. Form validation is done on the server side and error message is displays on the jsp page. To display the error on the jsp page tag is used. The tag displays all the errors in one go. To create text box is used in jsp page.
e.g.

Above tag creates text box for entering the address. The address is retrieved from and later stored in the property named address in the form-bean. The tag Save creates the submit button and the tag Cancel is used to create the Cancel button.
Add the following line in the index.jsp to create a link for testing the Address.jsp form:
Test the Address Form
In this lesson you learned how to create data entry form using struts, validate and finally send process the business logic in the model part of the struts.

Leave a Reply

You must be logged in to post a comment.