Succeeding With Struts: Dynamically Sized Forms
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;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.:
/**
* 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");
}
}
Fisher,Carrie:LeiaAlso 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.
Ford,Harrison:Han
Hamill,Mark:Luke
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