1、empty运算符
EL 表达式的 empty 运算符可以用于判断一个对象是否为空,包括 null、空字符串、空集合、空数组和空 Map 等情况。该运算符返回一个布尔值,即 true 或 false。
empty 运算符的语法为 ${empty 对象},其中的 对象 可以是任意类型的表达式,如变量、属性、方法调用等。
// 在 Servlet 中设置属性
request.setAttribute("str", ""); // 空字符串
request.setAttribute("list", new ArrayList<>()); // 空列表
request.setAttribute("array", new int[0]); // 空数组
request.setAttribute("map", new HashMap<>()); // 空 Map
// 在 JSP 中使用 empty 运算符判断对象是否为空
<!-- 判断空字符串 -->
<% if ("".equals(request.getAttribute("str"))) { %>
<p>字符串为空。</p>
<% } %>
<!-- 判断空列表 -->
<c:if test="${empty list}">
<p>列表为空。</p>
</c:if>
<!-- 判断空数组 -->
<c:if test="${empty array}">
<p>数组为空。</p>
</c:if>
<!-- 判断空 Map -->
<c:if test="${empty map}">
<p