-------- Original Message -------- Subject: [MVC-Programmers] [Struts Tips] #6 - Use an array to capture multiple parameters Date: Tue, 09 Apr 2002 07:46:36 -0400 From: Ted Husted <[EMAIL PROTECTED]> Reply-To: [EMAIL PROTECTED] Organization: Apache Software Foundation / Jakarta Newsgroups: MVC-Programmers
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 what the db object did behind the scenes, and the scale of the application, this could too simple to 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 just 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"> <TR> <TH>name</TH> <TH>category</TH> </TR> <TR> <TD><bean:write name="row" property="name"/></TD> <TD> <%-- REMEMBER TO SPECIFY THE ITERATE ID AS THE NAME --%> <html:select name="row" property="category"> <html:option value="ART">Art</html:option> <html:option value="AUT">Automotive</html:option> <%-- ... --%> </html:select> <%-- REMEMBER TO SPECIFY THE ITERATE ID AS THE NAME --%> <html:hidden name="row" property="key"/> </TD> </TR> </logic:iterate> <TR> <TD colspan="2"> <html:submit>SAVE CHANGES</html:submit> </html:form> </TD> </TR> </TABLE> Now when they submit the form, it will include several category=XYZ&key=123 tuples in the request. Struts will convert the category and key entries to individual arrays, which your business logic can then use to make the appropriate update. HTH, Ted. ----- About Ted. Ted Husted is an active Struts Committer and moderates the Struts mailing list and the JGuru Struts FAQ. _______________________________________________ MVC-Programmers mailing list [EMAIL PROTECTED] http://www.basebeans.com:8081/mailman/listinfo/mvc-programmers