JSTL:检查字符串是否为空

Daa*_*lie 13 java jsp jstl

我正在尝试在JSTL中测试会话属性是否为空.但是该属性为空JSTL将其视为非空属性.

这是我试图用JSTL替换的硬编码.此代码工作正常:

<% if (request.getAttribute("error") != null) { %>
    <div class="alert alert-danger">
        <strong>Oh snap, something's wrong, maybe the following error could help you out?<br /></strong>
        <%= request.getAttribute("error")%>
    </div>
<% } %>
Run Code Online (Sandbox Code Playgroud)

这就是我用JSTL取代它的方法.选中时,error-attribute不为空,但为空.

<c:if test="${not empty sessionScope.error}">
    <div class="alert alert-danger">
        <strong>Oh snap, something's wrong, maybe the following error could help you out?<br /></strong>
        <c:out value="${sessionScope.error}" />
    </div>
</c:if>
Run Code Online (Sandbox Code Playgroud)

gya*_*anu 14

添加JSTL库并声明核心taglib:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Run Code Online (Sandbox Code Playgroud)

JSTL相当于

<% if (request.getAttribute("error") != null) { %>
Run Code Online (Sandbox Code Playgroud)

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