Services | Updates | Contact
Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Create a ToolBar in Java
In this section, you will learn how to create toolbar in java. Swing provides a most important feature for creating an i
 
pdf landscape
Here, you will see how to pdf landscape in java.
 
More Tutorials...

Search All Tutorial
Top Search: Loan Struts Open Source

Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

 
 
Struts

 
Comments
 
 

 

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 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();
      
      ifgetName() == null || getName().length() ) {
        errors.add("name",new ActionMessage("error.name.required"));
      }
      ifgetAddress() == null || getAddress().length() ) {
        errors.add("address",new ActionMessage("error.address.required"));
      }
      ifgetEmailAddress() == null || getEmailAddress().length() ) {
        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 responsethrows 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:

<form-bean
name="AddressForm"
type="roseindia.net.AddressForm"/>

Add the following line in the struts-config.xml file for handling the action "/Address.do":

<action
     path="/Address"
     type="roseindia.net.AddressAction"
     name="AddressForm"
     scope="request"
     validate="true"
     input="/pages/Address.jsp">
    <forward name="success" path="/pages/success.jsp"/>
</action>

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 <html:errors/> tag is used. The <html:errors/> tag displays all the errors in one go.  To create text box <html:text .../> is used in jsp page. 

e.g.

<html:text property="address" size="30" maxlength="30"/>

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 <html:submit>Save</html:submit> creates the submit button and the tag <html:cancel>Cancel</html: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:

<html:link page="/pages/Address.jsp">Test the Address Form</html:link>

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.

 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

96 comments so far (post your own) View All Comments Latest 10 Comments:

hi
i want simple example for jsf in jboss with eclips so please give me replay soon.

Posted by agiurpadeep on Tuesday, 04.22.08 @ 10:17am | #57471

I am interested to participant in this site

Posted by Madhu on Wednesday, 03.19.08 @ 13:04pm | #53297

when i was execute above example its correct for address.jsp when i was click in the save button it is not goes to success.jsp and

server showing error for unable to read to ther 'success'.in last line pls tell me the answer

Posted by Tikam on Saturday, 02.23.08 @ 13:29pm | #49586

I am a Struts programmer at Net-temps,USA and this site is so helpful that i cant describe more.

This site is really really wonderful

Thanks.Keep up the good work

Posted by Nitin on Wednesday, 02.20.08 @ 16:17pm | #49206

please explain how to work struts project in eclipse3.0 IDE,tell me the step-by-step procedure of adding struts plugins to eclipseIDE

Posted by periasamy on Tuesday, 02.19.08 @ 05:15am | #48941

its must be ActionError insted of ActionMessage

ex: errors.add("name",new ActionError("error.name.required"));

and the message must be defined in the MessageResources.properties file...

ex: error.name.required= Name Required(Error Message).

Posted by varun on Monday, 02.11.08 @ 21:43pm | #47923

i am trying to build this in net beans6.0.
the save ans cancel are not working and validation as well need some help.
if some body have the working code pls tell me..

HTTP Status 404 - Servlet action is not available

type Status report

message Servlet action is not available

description The requested resource (Servlet action is not available) is not available.
Apache Tomcat/6.0.14

Posted by novicepro on Tuesday, 02.5.08 @ 07:51am | #47252

How to use reset(), defined in above ActionForm class?What are the attributes that i have to mention in struts-config xml file, to use this reset() method?

Posted by saritha on Tuesday, 01.29.08 @ 12:28pm | #46475

what is the difference between page and pagecontext in jsp

Posted by mahendar reddy on Friday, 01.18.08 @ 12:25pm | #45506

thanks for the tutorials

Posted by kurt on Wednesday, 01.2.08 @ 21:25pm | #44380

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

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

Copyright © 2007. All rights reserved.