Struts Tip #6 - Use an array to capture multiple parameters

Ted Husted



In a perfect universe, our users would happily subscribe to the One True Workflow. In every case, we could list some records, select a record to view or edit, and list some records again. But in real life, users often want to do unreasonable things, like edit 17 records at once.

Usually, we'd already have a business procedure for saving one copy of the record. Perhaps something like this

String key= form.getKey();
String category = form.getCategory();
db.updateCategory(new Integer(key),category);

A very simple way to convert this to "multiple use" would to do something like this:

String[] key = form.getKey();
String[] category = form.getCategory();
for (int i=0; i<category.length; i++) { db.updateCategory(new Integer(key),category); }

Depending on what the db object did behind the scenes, and the scale of the application, this code fragment could too simple to be efficient, but for many small intranet applications, it could work just fine.

Of course, that still leaves the problem of where to get an array of keys and a matching array of categories.

Happily, HTTP supports submitting multiple parameters of the same name, and Struts will autopopulate a String array as easily as a single String. So, to represent the "multi-use" business operation in an ActionForm, we can do this:

private String[] key= null;
public String[] getKey() { return this.key; }
public void setKey(String[] key) { this.key = key; }

private String[] category = null;
public String[] getCategory() { return this.category; }
public void setCategory(String[] category) { this.category = category; }

Which is just the single-record version but with String[] instead of String.

In the JavaServer Page, the only real trick is to refer to the iterate id in the html form tags.

<TABLE><html:form action="/item/StoreCategory">
<logic:iterate name="RESULT" property="iterator" id="row">
<TD><bean:write name="row" property="name"/></TD>
<html:select name="row" property="category">
<html:option value="ART">Art</html:option>
<html:option value="AUT">Automotive</html:option>
<%-- ... --%>
<html:hidden name="row" property="key"/>
<TD colspan="2" align="right">

Now when they submit the form, it will include several category=XYZ&key=123 pairs in the request. Struts will convert the category and key entries into individual arrays, and the arrays can be used with your business objects to make the appropriate update.

HTH, Ted.


Struts Tips  are excerpts from the book Java Web Development with Struts. The tips released twice weekly on the MVC-Programmers List. To subscribe, visit BaseBean Engineering.

About Ted. Ted Husted is an active Struts Committer and co-author of Java Web Development with Struts and Professional JSP Site Design. Ted also moderates the Struts mailing list and the JGuru Struts FAQ.

Copyright Ted Husted 2002. All rights reserved.


MVC-Programmers mailing list




0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.