1

I have a form with a field that may have zero to multiple values for the named field, e.g.,:

<form ...>
<input type="hidden" name="browseId[]" value="3">
<input type="hidden" name="browseId[]" value="4">
<input type="hidden" name="browseId[]" value="8">
<input type="hidden" name="browseId[]" value="10">

<input type="text" name="browseId[]">
...
</form>

I do not seem to be able to get the variable browseId as an array, which is standard operating procedure in HTML, other languages, and I'm stumped. I'm actually first processing the form output in a validation method, using a DynaActionForm:

public static ActionMessages validatePlacement(DynaActionForm form) {
    String[] rootBrowseIds = (String []) form.get("browseId");
    ...
}

Here's the form bean and action definition in struts-config.xml:

<form-bean name="placementForm" type="org.apache.struts.validator.DynaValidatorForm">
    <!-- I've tried a few variations -->
    <!--<form-property name="browseId" type="java.lang.String"/>-->
    <!--<form-property name="browseIds" type="java.lang.String[]"/>-->
    <form-property name="browseId" type="java.lang.String[]"/>
    ...
</form-bean>

...

<action path="/admin/editPlacement"
        type="com.rc.mexp.action.admin.placementinventory.EditPlacementAction"
        name="placementForm">
    <forward name="success" path="/WEB-INF/pages/admin/placement/placementEdit.jsp"/>
    <forward name="error" path="/admin/managePlacementInventory.do"/>
</action>

It appears that only the last value, empty in this case, is being received by Struts. WTF?

Any ideas?

Is there a way to change my form-bean definition to include something like this? I'm not allowed to use the < and > characters inside the type:

<form-property name="browseId" type="java.util.Map<java.lang.String[]>"/>

Other stackoverflow Qs that I examined already:
retrieve multiple inputs of the same name from jsp to struts (does not seem relevant)
Multiple inputs with same name through POST in php
Struts 2 - pattern/strategy for multiple objects on the same page

Community
  • 1
  • 1
barclay
  • 4,362
  • 9
  • 48
  • 68

1 Answers1

1

You're accessing the form field incorrectly, you should be using getStrings("browseId") since you're trying to get multiple strings.

Your code won't even compile for me, I'm not sure why you don't get a class cast exception.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • @kewpiedoll99 Oh, never mind; you're using `get`, not `getString`. Why aren't you using `getStrings`? This all works fine for me. – Dave Newton Nov 11 '11 at 16:36
  • Thanks - getStrings("browseId") worked. (I didn't know about it.) – barclay Nov 11 '11 at 16:42
  • @kewpiedoll99 Cool; glad you got it working. (I had to look it up myself; couldn't remember--been a loooong time since I did anything in Struts 1; hope you're being forced to use it instead of choosing it!) – Dave Newton Nov 11 '11 at 16:43