【问题标题】:How to compare 2 values using jstl如何使用 jstl 比较 2 个值
【发布时间】:2014-05-30 05:07:54
【问题描述】:

我在控制器中有列表

List l=serviceClientServiceImpl.getSavedParents(clientId); 
uiModel.addAttribute("savedParents",l);
List<Parent> parentList = 
(List<Parent>) serviceClientServiceImpl.getParents("Parent",Long.valueOf(clientId)); 
uiModel.addAttribute("parentList", parentList);

我正在 jsp 中检索它,如下所示

<ul style="max-width: 300px;" class="nav nav-pills nav-stacked" id="menuId"> 
<c:forEach items="${parentList}" var="test"> 

<li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" > 
<input type="checkbox" value="${test.id}">${test.name }</a></li> 


</c:forEach> 
</ul> 
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 

<c:forEach items="${savedParents}" var="test1"> 
<ul style="max-width: 300px;" class="nav nav-pills nav-stacked"> 
<li class="active"><a href="#" value="${test1.id}" onclick="return getQuestions(this);"> 
<input type="checkbox" value="${test1.id}"></a></li> 

</ul> 
</c:forEach>

现在我想合并两者和哪里 test.id 等于 test1.id 然后我想添加一个 html class="active"

我尝试了以下方式

<ul style="max-width: 300px;" class="nav nav-pills nav-stacked"  id="menuId">
<c:forEach items="${parentList}" var="test">
<c:forEach items="${savedParents}" var="test1">
<c:when test3="${test.id}==${test1.id }">
  <li ><a href="#" class="active" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
  <input type="checkbox" value="${test.id}">${test.name }</a></li>

</c:when>
<c:otherwise>
  <li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
  <input type="checkbox" value="${test.id}">${test.name }</a></li>
</c:otherwise>

</c:forEach>

  <%-- <li ><a href="#" value="${test.id}-${test.category}" id="${parent.id}" onclick="return getQuestions(this);" >
  <input type="checkbox" value="${test.id}">${test.name }</a></li> --%>


</c:forEach>
 </ul>
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

但我在 eclipse 中遇到错误

Multiple annotations found at this line:
    - Missing required attribute 
     "test"
    - Undefined attribute name 
     "test3"
    - Missing required attribute 
     "test"
    - Undefined attribute name 
     "test3"

任何机构都可以帮助我吗?

【问题讨论】:

    标签: spring jsp jstl


    【解决方案1】:

    尝试以这种方式比较字符串

    <c:when test="${test.id == test1.id }">
    

    您也可以使用eq 代替==

    <c:when test="${test.id eq test1.id }">
    

    【讨论】:

    • 感谢+1的回答
    • 我还有一个问题。你能帮帮我吗?
    • 确定这是同一个问题还是不同的问题?
    • 与本题无关,暂不讨论。你能来chat.stackoverflow.com/rooms/44929/java-and-jquery-beginners
    • 如果此问题已解决,请尝试接受答案。如果您不接受答案,人们往往不会回答您的问题,这是在开放论坛中要记住的提示。干杯!
    【解决方案2】:

    应该是&lt;c:when test= 而不是&lt;c:when test3=

    也可以试试c:if

    <c:set var="id1" value="2" />
    <c:set var="id" value="2" />
    
    <c:if test="${id == id1 }">
        Equal
    </c:if>
    

    <c:set var="id1" value="2" />
    <c:set var="id" value="2" />
    
    <c:choose>
        <c:when test="${id == id1 }">
            Equal
        </c:when>
        <c:otherwise>
            Not equal
        </c:otherwise>
    </c:choose>
    

    阅读更多JSTL Core conditional Tag

    注意:

    • 完整的条件表达式必须包含在单个${}
    • c:when 标签应包含在 c:choose 标签内

    &lt;c:choose&gt; 的工作方式类似于 Java 的 switch 语句,它允许您在多个备选方案中进行选择。在 switch 语句有 case 语句的地方,&lt;c:choose&gt; 标签有 &lt;c:when&gt; 标签。 switch 语句有 default 子句来指定默认操作,类似的方式 &lt;c:choose&gt;&lt;c:otherwise&gt; 作为默认子句。

    【讨论】:

    • 感谢您的回答。您能否分享一些观点,为什么我应该将 c:when 更改为 c:if 以及为什么我应该使用 test
    • 无需切换。我只是告诉你错误。如果标记也是正确的测试方法,请查看测试部分。
    • 我按照你的方式做了,但它给了我错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多