Tutorial: Creating DynaActionForm example in Struts

      

apache-struts-logo
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>

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">

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");
...
...
}

Facebook  Twitter      Stumbleupon  Delicious
  

5 Comments on “Tutorial: Creating DynaActionForm example in Struts”

  • Sarah wrote on 13 March, 2009, 16:16

    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

  • Viral Patel wrote on 13 March, 2009, 16:36

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

  • Chaitanya Yeluri wrote on 21 June, 2009, 7:16

    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.

  • Viral Patel wrote on 21 June, 2009, 13:25

    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.

  • Majid wrote on 2 December, 2009, 11:08

    I have a question please :

    how to do a dynaActionForm with combobox populated from database. ?

    thanks

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.