46
<c:if test="${param.username}" >
</c:if>

如何检查 param.username 是否存在?

4

3 回答 3

73

使用not empty支票。

<c:if test="${not empty param.username}" >
</c:if>

编辑:如果您有表单的参数?username(无值),使用起来更安全${param.username ne null}

于 2012-06-07T13:22:32.410 回答
43

如果要检查参数是否存在,只需测试它是否不为空,在您的情况下:

<c:if test="${param.username != null}"></c:if>



更广泛的解释:

如果你想检查:

  • 如果 yourParam 存在/不为空:

    <c:if test="${param.yourParam != null}"></c:if>

  • 如果 yourParam 不存在/为空

    <c:if test="${param.yourParam == null}"></c:if>

  • 如果 yourParam 不为空(非空字符串且非空)

    <c:if test="${!empty param.yourParam}"></c:if>

  • 如果 yourParam 为空(空字符串或 null)

    <c:if test="${empty param.yourParam}"></c:if>

  • 如果 yourParam 评估为“真”

    <c:if test="${yourParam}"></c:if>

  • 如果 yourParam 评估为“假”(“真”以外的字符串)

    <c:if test="${!yourParam}"></c:if>

于 2014-03-14T10:30:45.660 回答
5

如果我可以添加评论...

为了测试请求参数“username”是否在JSP页面“a-jsp.jsp”中不存在,我们可以在页面“a-jsp.jsp”中写一个“if”子句:

<c:if test="${empty param['username']>
...
</c:if>

如果请求的 URL 是,我们将遍历那个“if”子句:

http://server/webapp/a-jsp.jsp

或者

http://server/webapp/a-jsp.jsp?username=

如果请求的 URL 是,我们不会:

http://server/webapp/a-jsp.jsp?username=foo

于 2013-09-24T14:46:55.563 回答