[英]JSTL set a variable to the length of a string
我有一些代碼試圖在jstl中獲取xml節點值的長度。
<x:forEach select="$person/tel" var="phone">
<x:if select="$phone/@type='office'">
<c:set var="length" value=${fn:length("${phone}")}/>
<c:out value="${length}"/>
</x:if>
</x:forEach>
在調用該函數時出現錯誤。
“ org.apache.jasper.JasperException:/edit.jsp(行:87,列:35)應使用引號”
我需要獲取長度,因為長度決定了我解析它並將其寫入頁面的方式。
我不確定是否有辦法做到這一點,或者我是否正在正確地做到這一點。
更新的代碼:
<x:forEach select="$person/tel" var="tel">
<x:if select="$tel/@type='office'">
<x:set var="number" select="$tel/phone"/>
<c:set var="length" value="${fn:length(number)}"/>
<c:out value="${length}"/>
</x:if>
</x:forEach>
問題是您不能在fn:length()
使用"${phone}"
。
正確的方法應該是:
<c:set var="length" value=${fn:length(phone)}/>
想通了。 問題是:
<x:set var="number" select="$tel/phone"/>
不會設置字符串,而只是XML節點的表示。 要將節點內容轉換為字符串,我做了:
<c:set var="phonenumber">
<x:out select="$number"/>
</c:set>
x:out將節點內容轉換為字符串,然后將其包裝在c:set中以設置變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.