Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if parameter exists in Expression Language [duplicate]

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

How do I check if param.username exists??

like image 303
Borut Flis Avatar asked Jun 07 '12 13:06

Borut Flis


3 Answers

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}

like image 156
adarshr Avatar answered Nov 06 '22 05:11

adarshr


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>

like image 45
Łukasz Dumiszewski Avatar answered Nov 06 '22 04:11

Łukasz Dumiszewski


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

like image 6
Léa Massiot Avatar answered Nov 06 '22 05:11

Léa Massiot