Managing dynamic form elements in Struts

Those who are working with Struts, it could be a great problem if you have dynamic form elements in your form and you need to manage them. As they are dynamic, you dont know what are their name and you ant actually map these properties into your ActionForm bean. So how to manage these dynamic form elements, any idea? Take a look at this solution below.

First, Lets make your ActionForm bean like this

public class TestBean extends ActionForm {

public final Map values = new HashMap();

public void setValue(String key, Object value)
values.put(key, value);

public Object getValue(String key)
return values.get(key);

here comes the action in struts-config

Now we need to see how we can map dynamic properties in JSP pages.

Submit This

Please note that we add “value()” – thus it will access the Setter method setValue() in our ActionForm bean.

Finally, lets capture the submitted dynamic values in our Struts Action

public class TestAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

TestBean tb = (TestBean) actionForm;
httpServletRequest.setAttribute(“dynamic”, tb.getValue(“dynamic”)); //first dynamic
httpServletRequest.setAttribute(“dynamic2”, tb.getValue(“dynamic2”)); //second dynamic
return actionMapping.findForward(“success”);

Well, you can just go thru the HashMap in TestBean for all the dynamic values.


  1. Hi,
    yes same here it would be great if you can provide me an example code for the above.
    and i think in jsp we can do something like this

    Thanks & Regards,

  2. Hi,
    I started recently to develop struts thing i want to clear from the above.How the dynamically generated form fields are mapped to setValue()and getValue()?
    What should we give in struts-config file?

    Thanks & regards,

  3. Let me give an idea of what I am trying to accomplish and then see if you can feedback.

    Using struts (obviously) I am creating a reusable contact form. It is configured through an xml-config file that defines where the contact form goes and the smtp server it is to use, etc.. I want to set it up so that it does not have to be recompiled upon alternate use. This is one part of a set of components that make it easy for web designers to add functionality to websites they are building using the struts framework (whether they know it or not). i.e. most web designers to not have ide for compiling java classes. So, with those design goals in mind let me share with you my issue.

    The contact form by default captures the Name, Address, City, State, Phone Number, and any other basic contact form info needed. It has an ActionForm bean that captures the input of these predefined items and the data is then passed on to the Action and sent as an email to the recipient. My hangup is… I need to allow the flexibility of adding form elements to the form that are not defined in the ActionForm bean. I would also need to be able to pass the values back to the form in the event that a validation error occurs with the form. With the model the way it is I would have to specifically define the setters and getters in the ActionForm so that the Action could retrive the values of each form element and use them or route them back to the input form for errors or to another Action.

    Is there a way to have a generic getter/setter that can capture any non defined data? For example say a company cleans linen and they want to add a form element that asks for the type of detergent the contactee prefers. The added form element is called “preferredDetergent” yet it is not specifically defined in my ActionForm bean. First, how would I avoid getting an error that says preferredDetergent is not defined in xxxForm? Next, how would I then capture that value in the ActionForm for use in error checking and/or usage in the Action class?


Leave a comment

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