【问题标题】:How to check list.contains in jsp using struts 2 tags如何使用struts 2标签检查jsp中的list.contains
【发布时间】:2014-06-26 10:48:05
【问题描述】:

我需要显示一系列复选框,如果条件成功,我需要选中它们。下面是我的代码。我有一个 hashmap 的区域和一个数组列表的 SelectedRegions。我正在遍历我的区域地图,并在其旁边显示带有文本的复选框作为我的区域地图中的值。现在,在迭代时,如果区域映射的值在数组列表中可用,我需要选中该复选框。否则取消选中。我尝试了如下所示的。但它不起作用。

<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 条件不起作用。能不能请你 让我知道如何做到这一点?

【问题讨论】:

  • 有什么理由不使用 struts2 标签作为输入?还有,我没用过struts2,但不应该是#{condition}而不是#condition吗?
  • @AnthonyGrist:绝对不是#{...}。 user1321648:你会在课堂上怎么做?
  • 我没有使用 struts 标签,因为它不允许我进行自定义。我需要并排显示一个带有两个复选框的下拉菜单。我无法通过使用 struts 2 输入标签来实现这一点。
  • 我也尝试使用条件 - 但它没有用。
  • @aleksandr M:在我的课堂上,我会说 for (Map.Entry entry : map.entrySet()) { if(myList.contains(entry.getValue())) // ... }

标签: java jsp struts2 ognl


【解决方案1】:

确实有很多方法可以做到这一点。您还应该查看&lt;s:checkbox/&gt; 和&lt;s:checkboxlist/&gt; 标签;

还请注意,要遵循 DRY,您可以仅将 &lt;s:if&gt; 放在 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>

【讨论】:

  • 感谢安德里亚 Ligios。它就像一个魅力。我用这个-
  • 太好了,很高兴有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多