检查表达式语言中是否存在参数

Bor*_*lis 42 parameters jsp jstl el

<c:if test="${param.username}" >
</c:if>
Run Code Online (Sandbox Code Playgroud)

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

ada*_*shr 66

使用not empty支票.

<c:if test="${not empty param.username}" >
</c:if>
Run Code Online (Sandbox Code Playgroud)

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

  • 如果`host /?parameter`怎么办?它存在但是空的. (3认同)

Łuk*_*ski 40

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

<c:if test="${param.username != null}"></c:if>
Run Code Online (Sandbox Code Playgroud)



更广泛的解释:

如果你想检查:

  • 如果yourParam存在/不为null:

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

  • 如果yourParam不存在/为null

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

  • 如果yourParam不为空(不是空字符串而不是null)

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

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

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

  • 如果yourParam评估为'true'

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

  • 如果yourParam评估为'false'(字符串不是'true')

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


Léa*_*iot 5

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

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

<c:if test="${empty param['username']>
...
</c:if>
Run Code Online (Sandbox Code Playgroud)

如果请求的 URL 是:

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

或者

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

如果请求的 URL 是:

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