Struts DynaActionForm

In this tutorial you will learn how to create Struts DynaActionForm.

Struts DynaActionForm

1Struts 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" 
   <form-property name="name" type="java.lang.String"/>
   <form-property name="address" type="java.lang.String"/>
   <form-property name="email" type="java.lang.String" />

Adding action mapping

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

<action path="/DynaAddress" type=""

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


Creating Action Class

Code for action class is as follows:

@author Deepak Kumar
* @Web
* @Email [email protected]
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(""));
  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
  //Forward the page
  return mapping.findForward("success");
  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">
<title><bean:message key="welcome.title"/></title>
<body bgcolor="white">
<html:form action="/DynaAddress" method="post">
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>

<td align="left" colspan="2">
<font color="red"><html:errors/></font>

<td align="right">
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
<td align="right">
<td align="left">
<html:text property="address" size="30" maxlength="30"/>

<td align="right">
E-mail address
<td align="left">
<html:text property="email" size="30" maxlength="30"/>

<td align="right">
<td align="left">


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

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

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.


  1. Validations using Struts 2 Annotations
  2. Struts 1.1 Tutorials
  3. CRUD application in hibernate annotation
  4. Understanding Spring Struts Hibernate DAO Layer
  5. DAO Layer explained
  6. Developing Forgot Password Form
  7. Welcome to the Apache Struts Tutorial
  8. Developing Simple Struts Tiles Application
  9. Understanding Struts Controller
  10. Struts Hibernate Integration
  11. Developing Struts PlugIn
  12. Developing Struts Hibernate and Spring Based Login/Registration Application
  13. Struts File Upload and Save
  14. Struts 2 Features
  15. Struts 2 - History of Struts 2
  16. Struts 2 Architecture - Detail information on Struts 2 Architecture
  17. Download and Installing Struts 2
  18. Struts 2 Hello World Application Example, Learn how to develop Hello World application in struts 2.
  19. Developing JSP, Java and Configuration for Hello World Application
  20. Struts Configuration file - struts.xml
  21. Introduction to Struts 2 Tags
  22. Struts Logic Tags: An Introduction
  23. Logic Empty Tag (...)
  24. Logic Equal Tag (...)
  25. Logic greaterEqual Tag (... )
  26. Logic LessEqual Tag (...)
  27. Logic Match Tag (...)
  28. Logic Present Tag (...)
  29. Struts2 Actions
  30. Static Parameter
  31. Accessing Session Object
  32. Access Request and Response
  33. Control Tags-If / Else If / Else
  34. Append Tag (Control Tags) Example
  35. Generator Tag (Control Tags) Example
  36. Generator Tag (Control Tags) Using Count Attributes
  37. Generator Tag (Control Tags) Using an Iterator with Id Attributes
  38. Iterator Tag (Control Tags) Example
  39. Merge Tag (Control Tags) Example
  40. Subset Tag (Control Tags) Example