jsp 尝试访问 hashmap 变量时出错

jsp error attempting to access hashmap variable

我正在将哈希图传递给 jsp 视图,如下所示:

@RequestMapping(value = "/query")
public String processRegistration(@ModelAttribute("query") Query query,
        Map<String, Object> model) {
...
HashMap<String, String> serviceRequestData = executeSelect(conn, stmt, query);
model.put("serviceRequestData", serviceRequestData);
...
}

并尝试访问哈希映射中键 "FN_Contact" 的值,如下在 jsp 页面中:

<c:choose>
    <c:when test="${empty ${serviceRequestData[FN_Contact]}}">
    </c:when>
    <c:otherwise>
        <h3>Results:</h3>
        <h5>FirstName:</h5>
        ${serviceRequestData[FN_Contact]}<br>
    </c:otherwise>
</c:choose>

在这个 jsp 中,我试图检查该值是否为空,如果是则什么都不做,否则打印数据。我收到以下语法错误:

 javax.el.ELException: Failed to parse the expression [${empty ${serviceRequestData[FN_Contact]}}]

你这样试试

<c:when test="${empty serviceRequestData[FN_Contact]}">

使用JSTL表达语言,

使用标准函数检查JSTL函数提供的长度

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

使用以下 jsp 个标签,

<c:choose>
<c:when test="${fn:length(serviceRequestData)>= 1}">
    // Place the code what you want to do

</c:when>
<c:otherwise>
    // Nothing to display

</c:otherwise>
</c:choose>