JSP 标签中使用 trim 函数判断字符串是否为空
在 JSP 标签中使用 trim 函数判断字符串是否为空的方法:
-
首先,需要在标签中使用 EL 表达式获取到需要判断的字符串,例如:${param.name}
-
然后,使用 JSTL 的 c:if 标签来判断字符串是否为空,如下所示:
<c:if test='${not empty param.name and not empty param.name.trim()}'> </c:if>
在 test 属性中,使用 not empty 来判断字符串是否为空,同时也使用 not empty 和 trim() 来判断去除空格后是否为空。
-
如果需要在字符串为空时进行相应的处理,可以使用 c:choose 和 c:when 标签,如下所示:
<c:choose> <c:when test='${empty param.name or empty param.name.trim()}'> </c:when> <c:otherwise> </c:otherwise> </c:choose>
在 test 属性中,使用 empty 来判断字符串是否为空,同时也使用 empty 和 trim() 来判断去除空格后是否为空。
原文地址: https://www.cveoy.top/t/topic/oYHG 著作权归作者所有。请勿转载和采集!