Check if parameter exists in Expression Language [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-20 09:12:45

问题


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

How do I check if param.username exists??


回答1:


Use the not empty check.

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

Edit: If you have a parameter of the form ?username (no value), it is safer to use ${param.username ne null}




回答2:


If you want to check if parameter exists, just test if it is not null, in your case:

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



Broader explanation:

If you want to check:

  • if yourParam exists/ is not null:

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

  • if yourParam does not exist/ is null

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

  • if yourParam is not empty (not empty string and not null)

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

  • if yourParam is empty (empty string or null)

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

  • if yourParam evaluates to 'true'

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

  • if yourParam evaluates to 'false' (string other than 'true')

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




回答3:


If I may add a comment...

To test if the request parameter "username" does not exist in the JSP page "a-jsp.jsp", we can write an "if" clause in the page "a-jsp.jsp":

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

We'll go through that "if" clause if the requested URL is:

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

or

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

We won't if the requested URL is:

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



来源:https://stackoverflow.com/questions/10932677/check-if-parameter-exists-in-expression-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!