January 20, 2021
Hot Topics:

Succeeding With Struts: Dynamically Sized Forms

  • By James M. Turner
  • Send Email »
  • More Articles »
Remember that the HashMap values populated during the setup will disappear as soon as the form displays, because the form is request-scoped rather than session-scoped.  What this means in particular to us is that all the Person objects go away.  So, if we attached the text field to the comment property of the Person bean, we'd get a null pointer exception when we submitted the form, because the Person object would no longer be in the HashMap (in fact, we will have been given a completely new and empty HashMap.)  So, instead, we need to store the comments in a seperate, parallel HashMap which stores them as simple strings.

There are a few things to notice in the code above.  First, because we're now iterating over HashMap entries, the values available from the c:forEach tag are in fact placeholders for the hash entries, with two properties.  The key property is the value used to reference the hash (the strings "1", "2", "3", etc in our case), and the value property which has the value stored under that key.  So, in this case, we must use the value property to get at the actual properties of the Person bean.

Also, we need to construct a valid Struts property field for the text box.  This is done using the JSTL extensions available in the html-el taglib.  In this case, we store the comments by a string composed of the last name, a comma, and the first name of the actor.

Finally, we need a new action to process the results:

package demo;

* Copyright 2004, James M. Turner.
* All Rights Reserved
* A Struts action that sends the new comments to the console

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.struts.validator.DynaValidatorForm;

public class ProcessHashFormAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

DynaValidatorForm df = (DynaValidatorForm) form;
HashMap hm = (HashMap) df.get("comments");

Iterator it = hm.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String comment = (String) hm.get(key);
System.out.println(key + ":" + comment);

return mapping.findForward("success");
Again, the big difference here is that things are being stored as HashMaps.  The code gets the keys (lastname,firstname), and displays the keys and comments on the console, i.e.:


Also notice that when control is returned to the JSP page, a blank table is printed.  This is because the HashMap we created in the setup action is gone now, and we didn't recreate it when processing the results.  You could keep that data around in a session variable, but then you're right back to where you were with the first solution.  Better to choose a key that lets you get at the backend objects when the form is submitted, and always recreate any other form data needed.

Which one is best?  The array-based solution offers the ability to keep everything in one bean, while the hash-based solution avoids any session-scoped data.  Which one works better for you will be the final decision.

NOTE: A WAR file containing all the code and libraries needed to run these examples can be found at http://www.blackbear.com/struts.war.

About the author

James Turner is the Director of Software Development for Benefit Systems, Inc. He is also a contributor to the Apache Struts project. He has two books out on web-facing Java technologies, MySQL and JSP Web Applications, and Struts Kick Start.

Page 2 of 2

This article was originally published on March 4, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date