Struts2:无法为 struts select 标签设置多个默认 select 离子

Struts2: Unable to set multiple default selections for struts select tag

<s:select  name="carTypeIds"
           list="carTypes"
           listKey="carTypeId" 
           id="selectTypes"
           key="carTypeId" size="4"
           listValue="name"
           headerKey=""
           headerValue="All"
           multiple="true"
           value="%{carTypeIds}"
           label="Car Types"
/>

此处 carTypes 是类型 Car 的列表,包含属性 carTypeId (long)名称(字符串).

carTypeIds 是一个 String 数组,其中包含我希望默认为 select 的 carTypeId。为什么它不起作用?当我做

<s:property value="%{carTypeIds}" />

我可以打印 carTypeId。 struts 的新手,如有任何帮助,我们将不胜感激。

你的代码看起来不错,除了 key 属性:它用于一次性预设 namevaluelabel 属性,所有相同的值。由于您需要不同的值并且已经预设了它们,因此删除 key 属性。来自 the docs:

key : Set the key (name, value, label) for this particular component

还要确保您的 carTypeIds 包含与您的密钥相同类型的对象,否则 equals 可能会失败(例如 Longint):

multiple : Creates a multiple select. The tag will pre-select multiple values if the values are passed as an Array or a Collection(of appropriate types) via the value attribute. If one of the keys equals one of the values in the Collection or Array it wil be selected

P.S:注意headerKey不能为空...