在我当前的页面上,我使用 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/