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
JSTL: Property Access
In this example we are going to show we can access the property by using the dot(.) and [ ] operator.
 
Making a Frame Non Resizable in Java
This program illustrates you how to make a frame non resizable. It means that the disabling the maximize button of the f
 
More Tutorials...


    Loan Information     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
 
 

 

Struts DynaActionForm

                         

In this tutorial you will learn how to create Struts DynaActionForm. We will recreate our address form with Struts DynaActionForm. DynaActionForm is specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties, without requiring the developer to create a Java class for each type of form bean. DynaActionForm eliminates the need of FormBean class and now the form bean definition can be written into the struts-config.xml file. So, it makes the FormBean declarative and this helps the programmer to reduce the development time.

In this tutorial we will recreate the add form with the help of DynaActionForm. It also shows you how you can validate use input in the action class. 

Adding DynaActionForm Entry in struts-config.xml

First we will add the necessary entry in the struts-config.xml file.  Add the following entry in the struts-config.xml file. The form bean is of org.apache.struts.action.DynaActionForm type. The <form-property/> tag is used to define the property for the form bean. We have defined three properties for our dynamic form bean.

<form-bean name="DynaAddressForm"   
         type="org.apache.struts.action.DynaActionForm">
         <form-property name="name" type="java.lang.String"/>
         <form-property name="address" type="java.lang.String"/>
         <form-property name="email" type="java.lang.String" />
</form-bean>

Adding action mapping

Add the following action mapping in the struts-config.xml file:

<action path="/DynaAddress" type="roseindia.net.AddressDynaAction"
     name="DynaAddressForm"
     scope="request"
     validate="true"
     input="/pages/DynaAddress.jsp">

    <forward name="success" path="/pages/success.jsp"/>
    <forward name="invalid" path="/pages/DynaAddress.jsp" />

</action>

Creating Action Class

Code for action class is as follows:

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;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;


public class AddressDynaAction extends Action
{
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse responsethrows Exception{

    DynaActionForm addressForm = (DynaActionForm)form;

    //Create object of ActionMesssages
        ActionMessages errors = new ActionMessages();
    //Check and collect errors
        if(((String)addressForm.get("name")).equals("")) {
         errors.add("name",new ActionMessage("error.name.required"));
        }

        if(((String)addressForm.get("address")).equals("")) {
         errors.add("address",new ActionMessage("error.address.required"));
        }

        if(((String)addressForm.get("email")).equals("")) {
         errors.add("email",new ActionMessage("error.emailaddress.required"));
        }
    //Saves the error
    saveErrors(request,errors);
    //Forward the page
    if(errors.isEmpty()){
    return mapping.findForward("success");
    }else{
    return mapping.findForward("invalid");
    }
  }
}

Creating the JSP file

We will use the Dyna Form DynaAddressForm created above in the jsp file. Here is the code of the jsp(DynaAddress.jsp) file.

<%@ 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="/DynaAddress" method="post">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>

<tr>
<td align="left" colspan="2">
<font color="red"><html:errors/></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="email" 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>

Add the following line in the index.jsp to call the form.

<li>
<html:link page="/pages/DynaAddress.jsp">Dyna Action Form Example</html:link>
<br>
Example shows you how to use DynaActionForm.
</li>

Building Example and Testing

To build and deploy the application go to Struts\strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the DynaAddress.jsp page. Without entering anything in the form and submitting the submit button, your browser should show the following out put.

                         


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

Current Comments

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

The method saveErrors(HttpServletRequest, ActionErrors) in the type Action is not applicable for
the arguments (HttpServletRequest, ActionMessages)

in action class execute method when i placed the above code it is giving me the above error. please suggest. thanks in advance.

Posted by gopikrishna on Wednesday, 04.2.08 @ 17:41pm | #55126

Is it possible to use validator plugIn with DynaActinForm in struts? If yes, can any body provide example for that? I tried but didn't succeeded.

Posted by Ravindra on Tuesday, 02.12.08 @ 10:22am | #47978

which one is better Action Form or Dyna Action

Posted by krishna on Thursday, 01.17.08 @ 17:44pm | #45447

all the examples are worth learning. In DynaActionForm is there any internal method called saveErrors(HttpServletRequest, ActionMessages).

If Not please, in Struts DynaActionForm section explain saveErrors method in action class AddressDynaAction.

Thanks & Regards,
sathish.g.

Posted by sathish kumar.g. on Monday, 01.7.08 @ 14:38pm | #44704

This information provided by you is very useful. and you keep on updadting these technologies.

Posted by srividya on Thursday, 01.3.08 @ 10:21am | #44416

Very Nice Example !!!

Posted by Sandeep Natoo on Friday, 12.7.07 @ 17:08pm | #41544

are u using strutsdyna actin

Posted by tejj on Saturday, 11.17.07 @ 12:15pm | #37613

IT is reaaly really good stuff. very much useful to learn, simple, easy to understand.

Posted by Geetha on Thursday, 09.6.07 @ 23:11pm | #25319

hii,
I want codes of a demo struts project to know struts briefly.Plz help me.It's urgent

Posted by Manas on Wednesday, 08.8.07 @ 18:15pm | #22947

every thing fine in my logoin form that contains name,adderss,email,and my bean class contain setrs and geters,reset().i have done my action class. modifiy the strtus cinfig.xml my application did not work plz help me

Posted by vidyasagar on Tuesday, 08.7.07 @ 13:08pm | #22843

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.