jsp - JSTL 条件检查

标签 jsp jstl el

在我当前的页面上,我使用 JSTL 来检查数据是否可用于我的表单。我面临的问题是“如果没有数据,我也看不到文本字段”。我可以使用和标签来解决它,但这将需要大量的 if else if else 类型的代码贯穿整个页面。谁能建议我一个更好的清洁解决方案来解决这个问题?

<c:if test="${salesData!=null}">
  <c:if test="${fn:length(salesBundle.salesArea) > 0}">
  <input type="text" id="sales_area" class="salesManagerStyle">
  </c:if>
</c:if>

最佳答案

您可以在 test 中有多个条件.

<c:if test="${salesData != null && fn:length(salesBundle.salesArea) > 0}">
    <input type="text" id="sales_area" class="salesManagerStyle">
</c:if>

但您也可以使用 empty关键字进行空检查和长度检查。
<c:if test="${not empty salesData.salesArea}">
    <input type="text" id="sales_area" class="salesManagerStyle">
</c:if>

这是你现在能得到的最好的东西。如果您需要在页面的其他地方重复使用相同的条件,那么您也可以通过 <c:set> 保存它。 .
<c:set var="hasSalesData" value="${not empty salesData.salesArea}" />
...
<c:if test="${hasSalesData}">
    <input type="text" id="sales_area" class="salesManagerStyle">
</c:if>
...
<c:if test="${hasSalesData}">
    Foo
</c:if>

关于jsp - JSTL 条件检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219730/

相关文章:

jsp - 如何在调用错误消息的位置的openam存储库中找到?

java - 表达语言 : how to simplify this statement ("in"-like clause needed)

java - 将复杂集合与 c :foreach in JSP 一起使用

java - jSTL - 如何将表达式语言变量传递给 JavaScript 警报?

java - 了解工作文件上传解决方案

java - 使用 start.spring.io 的 spring 项目 stub 在没有 xml-conf 的情况下交付 jsp View 的最快方法

java - 直接从 JSF/richfaces 访问基于 java 的 DOM 树

java - org.apache.jasper.JasperException : The attribute prefix [fn] does not correspond to any imported tag library

java - 允许执行此操作的框架的实用程序/模式/语言是什么

jsp - 如何使用 EL 在 JSP 中获取 request/session/servletcontext 属性?