Search code examples
htmlstrutshtml-select

save all options of struts select element


I would like to implement a dynamic select element using struts. I need to allow the user to add option elements to a select element on a page, then save all options when the form is submitted, not just the selected option. As a matter of fact, I do not even care about the selected option once submitted.

For instance, when a user enters a page, the select element is populated by a collection in the form bean. The user can then add, edit or remove options in the displayed select element (via jQuery, javascript). Then, the updated set of options are sent to the server when the user submits the form.

Here is where I am starting:

<html:select property="myList">
  <html:options property="myList"/>
</html:select>

Is there a neat clean way to do this with the struts tags?


Solution

  • The struts tag doesn't have anything to do with the problem. All it can do is generate an select box containing options retrieved from the form bean. The problem is that an HTML select box only submits its selected option(s). That's what it's for.

    If you want to submit all the options, make your select box multi-selection, and make sure that all the options are selected before submitting the form (using JavaScript, in the form submit event handler).

    Or if you want to keep the select box as it is, create a hidden field for every option of the select box before the form is submitted (still using JavaScript, in the form submit event handler).