Tutorial: Creating DynaActionForm example in Struts

We learned in this tutorial about creating a basic web application in Struts, We had used Struts Frameworks ActionForm to manage the form data. Let us see how to use DynaActionForm to manage the form data. DynaActionForm is also called as Dynamic Form Bean. These are extensions of Form Beans that allow you to specify their properties inside the struts configuration file instead of having to create a concrete class. To create a DynaActionForm, we have to make following entry in struts-config.xml file.
<form-beans> <form-bean name="LoginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String" initial="UserName"/> <form-property name="password" type="java.lang.String" initial="Password"/> </form-bean> </form-beans>
Code language: HTML, XML (xml)
While using ActionForm, we create an entry in struts-config.xml with <form-bean> tag. We specify the class name of ActionForm class. In case of DynaActionForm, we create a dynamic form bean by specifying properties in struts-config.xml itself. In above code snippet we have created a login form with two properties: username and password. We have also provided its initial values. Once you have define an entry for Form bean in struts-config.xml, you can use this form in any of your action using normal <action> tag.
<action path="/login" name="LoginForm" validate="true" input="/index.jsp" type="net.viralpatel.struts.helloworld.action.LoginAction">
Code language: HTML, XML (xml)
Hence when the login action will be called, the form data will be populated in DynaActionForm and bean object will be passed to execute() method of LoginAction. To fetch the form data from Form object use get() method of DynaActionForm class.
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm)form; String userName = (String)dynaForm.get("username"); String password = (String)dynaForm.get("password"); ... ... }
Code language: Java (java)

View Comments

  • I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

    Sarah

  • Hi Sarah,
    Thanks for the comments. I hope you will like the articles. Enjoy reading.... :)

  • Hi all,

    I am new to this DynaActionForm when I tried to execute the same code I got an exception like this :
    Cannot retrieve definition for form bean dynaCustomerForm on action /AddDynaCustomerAction.do

    please help me to resolve this issue.

  • Hi Chaitanya,
    Have you checked your struts-config.xml file? Does it have entry for <form-bean> tag? Check if it has proper DynaAction definition. Also check your JAR file for class org.apache.struts.action.DynaActionForm.

  • I had came across your website looking for examples for filters but i had found mant more interesting example which I think is a wonderful work done by you.

  • Hi Viral,
    can you show a different example for Dynaaction form.what above is a classical example.can you illustrate another example where we can use Dynaaction form.Assume you have 5 checkboxes with country name and you have to send the checkbox data to next page.how would you accomplish this using dynaction form ? Please reply.

    • @Deepak - Thanks for the suggestion. Sure I'll try to write more on such complex implementation of DynaAction Form.

  • hi,
    I am a mechanical engineer and just started my career as a software engineer,I started learning struts nd our lead has asked us to do some work on indexed properties in struts,i googld for about 4 hrs bt all i could find is either a sophisticated code or a broken code..can u plz help me findind some info abt dat,atleast some links where i can get some useful info abt indexed proprties.

  • Hi Viral,

    In a multi user application, is it possible that my dynaform sends the value of another user? I have to solve a bug like this, i'm trying to get dynaform information stored into the request parameter, because i got the feeling request parameter is more secured. But, i don't have any idea about how to get the dynaform's (dynaclass) information stored into the request parameter.

    Any advice?

  • Hi Viral,
    I have seen your spring 3-mvc tutorial very effective and step by step...
    But I am unable to find the same step by step examples for struts....like starting from hello program and then further...
    Can You please guide me??

Share
Published by
Viral Patel
Tags: ActionForm DynaActionForm Struts

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

5 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

5 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago