DynaActionForm Example With Struts

This tutorial will briefly describe the use of DynaActionForm in Struts 1 mvc framework with example. You can also download code or war file of the example from the provided links at the bottom.

You may be thinking of one think about the struts form beans as we have to create separate form beans for every action that accepts different form attributes. In this way you may have to code a lot of form beans in a real life application. Struts 1 have provided a special type of form that can take different types of form attribute dynamically. This special type of form class is DynaActionForm. In this post, we will go through an example to implement DynaActionForm in struts and will see it reduces tremendous time of development.

In our example, there is a registration form with four fields on which validations are also applied according to the requirement that each and every field is mandatory. The following will be our implementation steps :

  1. First of all, we will configure DynaActionForm as our form bean in struts-config.xml with dynamic properties that will be present in jsp form.
  2. After that we will create our action class (ResigtrationAction.java) and will put validation login in execute method.
  3. Then, we will create the jsp page (Registration.jsp) which will hold our registration form and also show errors occur during form processing. We will also create a success jsp page (Success.jsp) which will be shown to user if there will be no error with the form data provided by the user.
  4. At the last, we will configure our action class in struts-config.xml file.

Configuring DynaActionForm in struts-config.xml

Following code is showing the configuration done in struts-config.xml of DynaActionForm as our form with dynamic properties :

    <!-- DynaActionForm Bean for Dyna -->
    <form-bean name="dynaRegistrationForm" type="org.apache.struts.action.DynaActionForm">
        <form-property name="name" type="java.lang.String" />
        <form-property name="username" type="java.lang.String" />
        <form-property name="password" type="java.lang.String" reset="true"/>
        <form-property name="rePassword" type="java.lang.String" reset="true"/>

As the above code is showing, for creating our form bean we put class
org.apache.struts.action.DynaActionForm as type attribute and then we define our form properties one by one <form-properties> tag.
<form-property name=”name” type=”java.lang.String” /> :- here “name” indicates the property name and “type” tells the type of the property. DynaActionForm supports a number of types which gives flexibility to work with DynaActionForm in struts. Following are some type we can define :

  • java.math.BigDecimal
  • java.math.BigInteger
  • boolean and java.lang.Boolean
  • byte and java.lang.Byte
  • char and java.lang.Character
  • java.lang.Class
  • double and java.lang.Double
  • float and java.lang.Float
  • int and java.lang.Integer
  • long and java.lang.Long
  • short and java.lang.Short
  • java.lang.String
  • java.sql.Date
  • java.sql.Time
  • java.sql.Timestamp

reset=”true” : It says that the field ha to be reset while serving a different request. It works as the reset method of form beans works.


We have to use a different approach to access the values in our form bean in action class. Following is the code in execute method of ResigtrationAction class :

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
    DynaActionForm regisForm = (DynaActionForm)form;
    String name = regisForm.get("name").toString();
    String username = regisForm.get("username").toString();
    String pass = regisForm.get("password").toString();
    String rePass = regisForm.get("rePassword").toString();
    ActionErrors errors = new ActionErrors();
        errors.add("name", new ActionMessage("error.field.empty", "Name"));
        errors.add("username", new ActionMessage("error.field.empty", "Username"));
        errors.add("password", new ActionMessage("error.field.empty", "Password"));
        errors.add("rePassword", new ActionMessage("error.field.empty", "Retype Password"));
    if(errors.isEmpty() && !pass.equals(rePass))
        errors.add("rePassword", new ActionMessage("error.field.passNotSame"));
        return mapping.findForward("success");
        saveErrors(request, errors);
        return mapping.getInputForward();

We first typecast the ActionForm instance to DynaActionForm and the use “get” method in DynaActionForm to get the value of the fields. After that we create instance of ActionErrors and check the field values if any error exist, if so we add the error to ActionErrors object and save the errors to request using “saveErrors” method. If not error is found in data then user will be forwarded to success page.


As usual the Registration.jsp will contain a html form to take input from user. Here is the code in Registration.jsp page :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Registration Page</title>
        All Fields are mandatory :
        <html:form action="/registration">
            <tr><td colspan="2"><html:errors/></td></tr>
            <tr><td>Name : </td><td><html:text property="name"/></td></tr>
            <tr><td>Username : </td><td><html:text property="username"/></td></tr>
            <tr><td>Password : </td><td><html:password property="password"/></td></tr>
            <tr><td>Retype Password : </td><td><html:password property="rePassword"/></td></tr>
            <tr><td colspan="2"><html:submit value="Register Me"/></tr>

Registration.jsp contains a form with four properties and a submit button. One thing to notice here is : the value of “property” attribute of <html:text/> tag is same as the value defined in “name” attribute in DynaActionForm defined in struts-config.xml. Success.jsp will contain only a confirmation message for user how ever if you want to print any property value of dynaactionform, you can use ${dynaRegistrationForm.map.name} while using JSTL tags in your code.


struts-config.xml file will contain the following configuration for our example :

    <!-- ========== Form Bean Definitions ================================== -->
        <!-- DynaActionForm Bean for Dyna -->
        <form-bean name="dynaRegistrationForm" type="org.apache.struts.action.DynaActionForm">
            <form-property name="name" type="java.lang.String" />
            <form-property name="username" type="java.lang.String" />
            <form-property name="password" type="java.lang.String" reset="true"/>
            <form-property name="rePassword" type="java.lang.String" reset="true"/>
    <!-- ========== Action Mapping Definitions ============================= -->
        <action name="dynaRegistrationForm" path="/registration"
        type="com.raistudies.actions.ResigtrationAction" input="/Registration.jsp" scope="request">
            <forward name="success" path="/Success.jsp" />
    <message-resources parameter="ApplicationResources"/>

While run the example in tomcat 6 server, it will show the following form :

Registration Form with DynaActionForm

Registration Form with DynaActionForm

If we click on “Register Me” submit button without filling any data in form following page will come to user :

DynaActionForm Validation Errors

DynaActionForm Validation Errors

Then try with filling all the fields but do not put same value in “Password” and “Retype Password” field, it will show an error with following error page :

DynaActionForm Validation Error

DynaActionForm Validation Error

Now try with the all correct data, you will be forwarded to Success.jsp page:

DynaActionForm Success page

DynaActionForm Success page

That’s all from DynaActionForm :). Happy Coding.

You can download code or war file of the example from following links :

Source : Download

War : Download

Related Posts:

Leave a comment ?


  1. Absolutely, I wonder that for even those eiterprnse apps that make extensive use of frameworks whether they get any real benefit out of them.What’s the point in making something re-usable if you’re never going to re-use it?And if you don’t follow it through ALL THE WAY (unit testing, mocking, documentation etc) then chances are it’s costing you more to develop, and you’re never going to get the payback you should.

  2. What does this saveErrors(request,errors) do ?

    why we donot use this method in other examples i.e when we use validation using formbean class

    • The saveErrors method is used to add error message in struts context so that it can be shown in input form jsp.

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>