java - 如何使用struts 2标签检查jsp中的list.contains

标签 java jsp struts2 ognl

我需要显示一系列复选框,如果条件成功,我需要检查它们。下面是我的代码。我有一个 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/

相关文章:

java - 使用 Struts2 从服务器接收到重复的 header

java - 选择 Java 集合实现的经验法则?

java - 如何使用正则表达式选择文件路径

java - 运行index.jsp时无法显示表格

java - 在Struts 2 redirectAction中,我可以传递动态复杂对象而不进行类型转换吗?

java - 使用 Struts Select 标签指定选项标签 (listValue)

Java:如果我们必须自己实现抽象方法,实现多个接口(interface)如何实现多重继承?

java - 如何在 Canvas 上一张一张地绘制多个图像,就像带有淡入和淡出动画的幻灯片一样

javascript - struts2 optiontransferselect 从数据库中检索和显示值

java - 在java中读取陷入循环的文本文件