【JSP EL】<c:if> <c:foreach >EL表达式 获取list长度/不用循环,EL在List中直接获取第一项的内容/EL获取Map的键,Map的值

1.EL表达式 获取list长度

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:if test="${fn:length(list名字)>1}">

  中间该干嘛干嘛

</c:if>

2.不用循环,EL在List中直接获取第一项的内容

${list[0].属性}

3.EL获取Map的键,Map的值

<c:forEach items="${map名字}" var="k">
  <option value="${k.key }">${k.value}</option>
</c:forEach>

这样分别获取键和值。

4.<c:if  test="条件是相等或不想等">  情况说明

【注意】:如果遇到 == 不起作用,使用eq看是否起作用,一般情况下==可以满足任何类型的比较

首先,页面最上引入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

接着,在页面模拟一个数据,不用从后台传过来,不用那么麻烦了

  ①int值比较是否相等

  

<%int a = 12;request.setAttribute("a", a); %>
    <c:if test="${a eq 12}">
        该干嘛干嘛
    </c:if>

  或者

<%int a = 12;request.setAttribute("a", a); %>
     <c:if test="${a == 12}">
         该干嘛干嘛
      </c:if>

  ②int值比较不相等

<%int a = 12;request.setAttribute("a", a); %>
     <c:if test="${a != 12}">
          该干嘛干嘛
      </c:if>

  ③Integer值比较相等

<%Integer a = 12;request.setAttribute("a", a); %>
    <c:if test="${a eq 12}">
        该干嘛干嘛
    </c:if>

  或者

<%Integer a = 12;request.setAttribute("a", a); %>
      <c:if test="${a == 12}">
          该干嘛干嘛
      </c:if>

  

  ④Integer值比较不相等

<%Integer a = 12;request.setAttribute("a", a); %>
       <c:if test="${a != 12}">
            该干嘛干嘛
       </c:if>

  ⑤String值比较相等【注意,单引号或者双引号的问题】

<%String a = "凉凉";request.setAttribute("a", a); %>
    <c:if test="${a eq '凉凉'}">
          凉凉夜色思念为你成河
    </c:if>

<%String a = "凉凉";request.setAttribute("a", a); %>
      <c:if test="${a == '凉凉'}">
           凉凉夜色思念为你成河
      </c:if>

  ⑥String值比较不想等

<%String a = "凉凉";request.setAttribute("a", a); %>
      <c:if test="${a != '凉凉'}">
            凉凉夜色思念为你成河
       </c:if>

5.El表达式判空操作

<c:if test="${empty admin.name}">
或者
<c:if test="${not empty admin.name}">

  查看:http://www.cnblogs.com/sxdcgaq8080/p/8119186.html

原文地址:https://www.cnblogs.com/sxdcgaq8080/p/6434682.html