JSTL条件检查 [英] JSTL conditional check
问题描述
在当前页面上,我正在使用JSTL检查表单中是否有数据.我面临的问题是如果没有数据,我也不会看到文本字段".我可以使用和标记来解决它,但这将需要大量的代码来遍及整个页面.有人可以建议我为这个问题提供更好的解决方案吗?
On my current page I am using JSTL to check if data is available for my form. Problem I am facing is "if there is no data I am not seeing the text fields either". I can solve it using and tags but that would entail lot of if else if else kind of code all through the page. Can anyone suggest me a better cleaner solution to this problem?
<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
关键字同时执行 空检查和长度检查.
But you can also use the empty
keyword to do both a nullcheck and lengthcheck.
<c:if test="${not empty salesData.salesArea}">
<input type="text" id="sales_area" class="salesManagerStyle">
</c:if>
现在,这是您所能获得的最好的.如果您需要重用页面中其他位置相同的条件,则也可以通过<c:set>
保存它.
That's the best what you can get, now. If you need to reuse the same condition elsewhere in the page, then you could also save it by <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>
这篇关于JSTL条件检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!