Compruebe si el parámetro existe en el Lenguaje de Expresión [duplicar]


Esta pregunta ya tiene una respuesta aquí:

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

¿Cómo puedo comprobar si param.nombre de usuario existe??

Author: Ali Dehghani, 2012-06-07

3 answers

Utilice la comprobación not empty.

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

Editar: Si tiene un parámetro de la forma ?username (sin valor), es más seguro usar ${param.username ne null}

 57
Author: adarshr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-13 22:49:35

Si desea comprobar si el parámetro existe, simplemente pruebe si no es null, en su caso:

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



Explicación más amplia:

Si desea comprobar:

  • Si yourParam existe/ no es null:

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

  • Si yourParam no existe / es null

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

  • Si yourParam no está vacío (no cadena vacía y no null)

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

  • Si yourParam está vacío (vacío string o null)

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

  • Si yourParam evalúa a'true'

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

  • Si yourParam evalúa a ' false '(cadena distinta a'true')

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

 36
Author: Łukasz Dumiszewski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-06 05:51:41

Si me permite añadir un comentario...

Para probar si el parámetro de solicitud "username" no existe en la página JSP "a-jsp.jsp", podemos escribir una cláusula " if "en la página" a-jsp.jsp":

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

Vamos a ir a través de esa cláusula "if" si la URL solicitada es:

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

O

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

No lo haremos si la URL solicitada es:

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

 4
Author: Léa Massiot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-24 14:46:55