Oceń puste lub null znaczniki JSTL c

Jak mogę sprawdzić, czy String jest puste lub null, używając znaczników c z JSTL?

Mam zmienną o nazwie var1 i mogę ją wyświetlić, ale chcę dodać komparator, aby ją zweryfikować.

<c:out value="${var1}" />

Chcę zweryfikować, kiedy jest null lub empty (moje wartości to łańcuchy znaków).

 363
Author: Tom11, 2010-05-11

8 answers

Jak mogę sprawdzić, czy ciąg znaków jest null lub pusty przy użyciu znaczników c JSTL?

Możesz użyć słowa kluczowego empty w <c:if> za to:

<c:if test="${empty var1}">
    var1 is empty or null.
</c:if>
<c:if test="${not empty var1}">
    var1 is NOT empty or null.
</c:if>

Lub <c:choose>:

<c:choose>
    <c:when test="${empty var1}">
        var1 is empty or null.
    </c:when>
    <c:otherwise>
        var1 is NOT empty or null.
    </c:otherwise>
</c:choose>

Lub jeśli nie musisz warunkowo renderować kilku znaczników, a więc możesz sprawdzić je tylko wewnątrz atrybutu znacznika, możesz użyć operatora el conditional ${condition? valueIfTrue : valueIfFalse}:

<c:out value="${empty var1 ? 'var1 is empty or null' : 'var1 is NOT empty or null'}" />

Aby dowiedzieć się więcej o tych ${} rzeczach (wyrażenie Język , który jest odrębnym przedmiotem od JSTL), sprawdź tutaj .

Zobacz też:

 692
Author: BalusC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:41

Aby również sprawdzić pusty ciąg, proponuję wykonać

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:if test="${empty fn:trim(var1)}">

</c:if>

Obsługuje również Null

 20
Author: andro83,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-21 07:54:50

Jeśli zaznaczysz tylko null lub empty, możesz użyć opcji z domyślną: <c:out default="var1 is empty or null." value="${var1}"/>

 6
Author: Ankit Agarwal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-11 10:38:28

Ten kod jest poprawny, ale jeśli wpisałeś dużo spacji ( '' ) zamiast null lub empty string return false.

Aby to poprawić, użyj zwykłego wyrażenia (poniższy kod sprawdza, czy zmienna jest null lub pusta lub pusta tak samo jak org.Apacz.commons.lang.StringUtils.isNotBlank): {]}

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
        <c:if test="${not empty description}">
            <c:set var="description" value="${fn:replace(description, ' ', '')}" />
            <c:if test="${not empty description}">
                  The description is not blank.
            </c:if>
        </c:if>
 5
Author: Rija Ramampiandra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-13 07:25:06

Oto jeden liniowiec.

Operator trójdzielny wewnątrz EL

${empty value?'value is empty or null':'value is NOT empty or null'}
 5
Author: Sorter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-12 12:52:37

Możesz użyć

    ${var == null}

Alternatywnie.

 2
Author: Supun Dharmarathne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-30 09:28:47
In this step I have Set the variable first:

<c:set var="structureId" value="<%=article.getStructureId()%>" scope="request"></c:set>

In this step I have checked the variable empty or not:

 <c:if test="${not empty structureId }">
    <a href="javascript:void(0);">Change Design</a>
 </c:if>
 -1
Author: ASR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-28 07:12:34

Oto przykład jak zweryfikować int i ciąg znaków, które przekazujesz z kontrolera Java do pliku JSP.

MainController.java:

@RequestMapping(value="/ImportJavaToJSP")
public ModelAndView getImportJavaToJSP() {
    ModelAndView model2= new ModelAndView("importJavaToJSPExamples");

    int someNumberValue=6;
    String someStringValue="abcdefg";
    //model2.addObject("someNumber", someNumberValue);
    model2.addObject("someString", someStringValue);

    return model2;
}

Importujjsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<p>${someNumber}</p>
<c:if test="${not empty someNumber}">
    <p>someNumber is Not Empty</p>
</c:if>
<c:if test="${empty someNumber}">
    <p>someNumber is Empty</p>
</c:if>
<p>${someString}</p>
<c:if test="${not empty someString}">
    <p>someString is Not Empty</p>
</c:if>
<c:if test="${empty someString}">
    <p>someString is Empty</p>
</c:if>
 -1
Author: Gene,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-12 14:08:26