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>
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)
Get our Articles via Email. Enter your email address.

You may also like...

15 Comments

  1. Sarah says:

    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

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

  3. Chaitanya Yeluri says:

    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.

  4. 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.

  5. Majid says:

    I have a question please :

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

    thanks

  6. Rashmi says:

    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.

  7. Deepak says:

    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.

  8. chaitanya vardhan says:

    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.

  9. Florent says:

    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?

  10. sunil says:

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

  11. teena says:

    Hi ,
    I am fresher and whenever I have to start off working with a new concept in Java,I often visit your blog and find it extremely helpful.Could you please do a tutorial on ‘Primefaces(JSF) DYNAFORM’.??It would really help.
    Thanks,
    Teena

  12. Tharmaraj.A says:

    Hi Mr.Viral Patel
    I’m Tharma your tutorial is very easy and useful keep it up thank you

  13. Elangovan.H says:

    I have saw your struts programs it is very clear and easy to understand step by step explaination

  14. Mahesh says:

    Can you provide full code…

Leave a Reply

Your email address will not be published. Required fields are marked *