[struts]Write Action Form classes and Action classes

Write ActionForm classes: 

An ActionForm class captures input form data that is entered by an end user. An ActionForm calss is a JavaBean and each input form data field is mapped to a property of the ActionForm class. Consequently you write getter and setter methods for each of the properties.

  In the example below, there is only one ActionForm class defined and it is SubmitForm class. In the SubmitForm.java code shown below, you see getter and setter methods of lastname, address, sex, married, and age properties.

  As was mentioned before, Struts framework creates ActionForm object instance and uses the setter methods to set the values of the properties with the input form data values that are received from the browser and then pass it as an argument to the execute() method of the Action class.

  Another important piece of code you could have inside your ActionForm class is the validate() method, in which you provide input validation logic. In this example, the input validation logic is basically making sure the user entered somthing for each input field. In other words, if nothing is entered for address field, an ActionErrors object is created and returned to Struts framework.

    Example 1 (SubmitForm.java):

package submit;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public final class SubmitForm extends ActionForm {

  
/*  Getter and Setter methods of properties */

  
/* Getter and Setter methods of Last Name property */
  
private String lastName = "Your last name"// default value
  public String getLastName() {
    
return (this.lastName);
  }

  
public void setLastName(String lastName) {
    
this.lastName = lastName;
  }


  
/* Getter and Setter methods of  Address property */
  
private String address = null;
  
public String getAddress() {
    
return (this.address);
  }

  
public void setAddress(String address) {
    
this.address = address;
  }


  
/* Getter and Setter methods of  Sex property */
  
private String sex = null;
  
public String getSex() {
    
return (this.sex);
  }

  
public void setSex(String sex) {
    
this.sex = sex;
  }


  
/* Getter and Setter methods of  Married status property */
  
private String married = null;
  
public String getMarried() {
    
return (this.married);
  }

  
public void setMarried(String married) {
    
this.married = married;
  }


  
/* Getter and Setter methods of  Age property */
  
private String age = null;
  
public String getAge() {
    
return (this.age);
  }

  
public void setAge(String age) {
    
this.age = age;
  }


  
/* Input validation */

  
public ActionErrors validate(ActionMapping mapping,
         HttpServletRequest request) 
{

    
// Log the forms data
    servlet.log("Lastname:" + lastName);       
    servlet.log(
"Address:" + address);       
    servlet.log(
"Sex:" + sex);       
    servlet.log(
"Married:" + married);       
    servlet.log(
"Age:" + age);
  
    
// Check for mandatory data
    ActionErrors errors = new ActionErrors();
    
if (lastName == null || lastName.equals("")) 
      errors.add(
"Last Name"new ActionError("error.lastName"));
    }

    
if (address == null || address.equals("")) 
      errors.add(
"Address"new ActionError("error.address"));
    }

    
if (sex == null || sex.equals("")) 
      errors.add(
"Sex"new ActionError("error.sex"));
    }

    
if (age == null || age.equals("")) 
      errors.add(
"Age"new ActionError("error.age"));
    }

    
return errors;
  }


}

 

Write Action classes:   

  As was mentioned before, for each request, the execute() method of the Action class get invoked by the Struts framework. So the execute() method is the place where you want to provide business logic of your application. In the example below, the business logic is basically changing the last name into upper case. However, in practice, you could include more sophisticated business logic code that might access EJB's, backend database or Web services.

  Please note the arguments that are passed to the execute() method. They are ActionMapping object, ActionForm object, HttpServletRequest and HttpServletResponse objects.

  • ActionMapping: The ActionMapping class contains findForward("<outcome>") method that you can call within the execute() method of Action calss in order to select the next page to display. The findForward("<outcome>") method returns ActionForward object. the Struts framework uses the returned ActionForward object and navigation rules specified in the struts-config.xml file to make a next page selection. In the example below, the outcome that gets returned from the excute() method is always "success". And as you've seen above. In this example, the navigation rule in struts-config.xml says to display submit.jsp page when the outcome is "success" and display the same submit.jsp page when the outcome is "failure". So in this struts-submit example, the next page that gets selected for display is always submit.jsp page.
  • ActionForm: As was mentioned above, the ActionForm object captures the input data that are entered by the user. In this example, the ActionForm object is then cast to SubmitForm object. Then you can use getter methods of the SubmitForm object in order to access the data.

Example (SubmitAction.java):

package submit;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SubmitAction extends Action {

  
// The execute() method is where you provide your business logic
  public ActionForward execute(ActionMapping mapping,
                                              ActionForm form,
                                              HttpServletRequest request,
                                              HttpServletResponse response) 
{

    
//  Cast ActionForm object to SubmitForm type
    SubmitForm f = (SubmitForm) form; 

    
//  Retrieve the value of lastname field
    String lastName = f.getLastName(); 

    
//  Translate the lastname to upper case and save it Request scope
    request.setAttribute("lastName", lastName.toUpperCase());
    
    
//  Create and return ActionForward object with "success" outcome
    return (mapping.findForward("success"));
  }

}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章