我需要显示一系列复选框,如果条件成功,我需要检查它们。下面是我的代码。我有一个 hashmap 的区域和一个数组列表的 SelectedRegions。我正在迭代我的区域 map ,并显示复选框,其旁边的文本作为我的区域 map 中的值。现在,在迭代时,如果数组列表中存在区域映射的值,我需要选中该复选框。否则未检查。我尝试了如下所示的方法。但它不起作用。
<s:iterator value="regions">
<li>
<div class="listClass">
<s:if test="#regions.value==selectedRegions.value">
<input type="checkbox" id='myTheater' checked="checked" name="theaterCheckBox" class="theaterCheckBox" />
<s:property value="value" />
</s:if>
<s:else>
<input type="checkbox" id='myTheater' name="theaterCheckBox" class="theaterCheckBox" />
<s:property value="value" />
</s:else>
</div>
<div class="checkboxDiv">
<input type="checkbox" id="allFeatured" class="featuredCheckBox" />
</div>
</li>
</s:iterator>
不知怎的,我使用的 if 条件不起作用。能不能请你 让我知道如何实现这一目标?
最佳答案
确实有很多方法可以做到这一点。您还应该查看<s:checkbox/>
和<s:checkboxlist/>
标签;
另请注意,要遵循 DRY,您可以输入 <s:if>
周围checked="checked"
仅部分,因为其余部分都是相同的。
为了保持简单,按原样使用代码,一种方法是使用 OGNL 的 in
(包含)和 OGNL 的 {}
(列表投影)如下:
<s:iterator value="regions">
<li>
<div class="listClass">
<input type = "checkbox"
id = "myTheater"
<s:if test="%{value in selectedRegions.{value}}">
checked = "checked"
</s:if>
name = "theaterCheckBox"
class = "theaterCheckBox" />
<s:property value="value" />
</div>
<div class="checkboxDiv">
<input type="checkbox" id="allFeatured" class="featuredCheckBox" />
</div>
</li>
</s:iterator>
关于java - 如何使用struts 2标签检查jsp中的list.contains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24428474/