Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: The Struts ActionForm Class

The Struts ActionForm Class The ActionForm Class In this lesson you will learn about the ActionForm in detail. I will show you a good example of ActionForm. This example will help you understand Struts in detail. We will create user interface to

Tutorial Details:

accept the address details and then validate the details on server side. On the successful validation of data, the data will be sent to model (the action class). In the Action class we can add the business processing logic but in this case we are just forwarding it to the sucess.jsp.
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.*;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email roseindia_net@yahoo.com
*/
/**
* 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;
}
}
The above class populates the Address Form data and validates it. The validate() method is used to validate the inputs. If any or all of the fields on the form are blank, error messages are added to the ActionMapping object. Note that we are using ActionMessage class, ActionError is now deprecated and will be removed in next version.
Now we will create the Action class which is the model part of the application. Our action class simply forwards the request the Success.jsp. Here is the code of the AddressAction class:
AddressAction .java
package roseindia.net;
/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email roseindia_net@yahoo.com
*/
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" %>

<html:html locale="true">

<head>

<title><bean:message key="welcome.title"/></title>

<html:base/>

</head>

<body bgcolor="white">

<html:form action="/Address">

<html:errors/>

<table>

<tr>

<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>
<tr>
<td align="right">
Name
</td>
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
Address
</td>
<td align="left">
<html:text property="address" size="30" maxlength="30"/>
</td>
</tr>

<tr>
<td align="right">
E-mail address
</td>
<td align="left">
<html:text property="emailAddress" size="30" maxlength="30"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit>Save</html:submit>
</td>
<td align="left">
<html:cancel>Cancel</html:cancel>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
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
Build the application and click on the Test the Address Form link on the index page to test the newly created screen. You should see the following screen.
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.


 

Rate Tutorial:
http://www.roseindia.net/struts/strutsActionForms.shtml

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
The Struts ActionForm Class

View Tutorial:
The Struts ActionForm Class

Related Tutorials:

Boost Struts with
Boost Struts with XSLT and XML
 
Mix protocols transparently in Struts
Mix protocols transparently in Struts
 
Take command of your software
Take command of your software
 
Protect Web application control flow
Protect Web application control flow
 
Jump the hurdles of Struts development
Jump the hurdles of Struts development
 
Excellent tutorial on Struts and Tiles
Excellent tutorial on Struts and Tiles This tutorial assumes knowledge of Java, JDBC, Servlets, J2EE (with regards to Web applications) and JSP Struts in a holistic manner, minus the beads and crystals. The Tiles framework makes creating reusable pages
 
Struts best practices
Multiple options are available for solving problems with Struts. When deciding among these alternatives, the choice must be based on parameters such as the scale of work and availability of time.
 
Welcome to the Apache Struts Tutorial
This is the complete Struts Tutorial. Explains ActionForm Action Class Validation Framework.
 
Integrating Struts, Tiles, and JavaServer Faces
Integrating Struts, Tiles, and JavaServer Faces. Bring the power, flexibility, and manageability of the three technologies together.
 
Struts and Tiles aid component-based development
In the Java world, Struts is one of the best-known and most talked about open source embodiments of MVC.
 
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java Open Source Web Frameworks in Java Struts Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open
 
Collection of Large Number of Java Interview Questions!
Collection of Large Number of Java Interview Questions! Collection of Large Number of Java Interview Questions The Core Java Interview Questions The Jakarta Struts Interview Questions
 
Jakarta Struts Interview Questions
Jakarta Struts Interview Questions Jakarta Struts Interview Questions Q: What is Jakarta Struts Framework? A: Jakarta Struts is open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications.
 
Beginner to advance guide to the Apache Struts
Beginner to advance guide to the Apache Struts The Complete Apache Struts Tutorial This complete reference of Jakarta Struts shows you how to develop Struts applications using ant and deploy on the JBoss Application Server. Ant script is provided
 
The Struts ActionForm Class
The Struts ActionForm Class The ActionForm Class In this lesson you will learn about the ActionForm in detail. I will show you a good example of ActionForm. This example will help you understand Struts in detail. We will create user interface to
 
Struts Guide
Struts Guide Struts Guide This tutorial is extensive guide to the Struts Framework. In this tutorial you will learn how to develop robust application using Jakarta Struts Framework. This tutorial assumes that the reader is familiar with the web
 
Struts HTML Tags
Struts HTML Tags Struts HTML Tags Struts provides HTML tag library for easy creation of user interfaces. In this lesson I will show you what all Struts HTML Tags are available to the JSP for the development of user interfaces. To use the Struts
 
Struts Validator Framework Tutorial with Example
Struts Validator Framework Tutorial with Example Struts HTML Tags Struts Validator Framework This lesson introduces you the Struts Validator Framework. In this lesson you will learn how to use Struts Validator Framework to validate the user
 

Understanding Struts Action Class 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
 
Understanding Struts Controller
Understanding Struts Controller Understanding Struts Controller In this section I will describe you the Controller part of the Struts Framework. I will show you how to configure the struts-config.xml file to map the request to some destination
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.