Problem: When using a select (s:select
) element that supports multiple selection (multiple="true"
) with option groups (s:optgroup
), if the field has values corresponding to elements of the option group elements, they will not default to selected.
Similar configurations that do work as expected:
- When the
list
s in thes:optgroup
are instead provided as the list for thes:select
element, the correct options will default to selected. - If the same, or different, lists is are to both
s:select
ands:optgroup
, the elements ins:select
will be correctly selected, but those ins:optgroup
will not. - When the
s:select
element instead supports only single selection (nomultiple="true"
), and the field (mySelections) is changed to an Integer instead of a List, the correct options will default to selected.
In all cases (both the problem case and the similar cases that work as expected), the selected elements are being correctly submitted to the action, as one would expect given it is just HTML at that point.
JSP that is causing the problem:
<s:select
name="mySelections"
list="#{9:'Z'}"
multiple="true"
size="5">
<s:optgroup
label="List A"
list="#{1:'A',2:'B',3:'C'}" />
<s:optgroup
label="List B"
list="#{4:'D',5:'E'}" />
</s:select>
On the off chance it was related to the UI theme, I tried against both "simple" (the one I actually use), and the default ("xhtml"), but the issue presents in both.
There is an open JIRA issue that looks related, in that the user's example does specify a multiple-select field, though the user does not identify if single-select works.
If this is a known issue, which the JIRA issue would suggest, does there exist a known workaround? However, it seems like this is something that would have been noted more frequently, if it were an issue with Struts2, and thus I am suspecting it is my error rather than the framework's that is causing the issue.
Differences between my actual case and the example above: In my actual case the list
s are properties on the action, instead of static, and s:select
list has no elements ({}
).
The version of Struts 2 is 2.3.16.2.
My expectation is that the suggested workaround would be to just generate the select
tag and its descendants using JSP. If so, are there any "gotchas" with regards to using a non-Struts tag in my interface?
<s:optgroup>
tag. I opened a jira issue for it: issues.apache.org/jira/browse/WW-4387. Not sure if WW-4367 is related or not.