2011-05-12 47 views

回答

11

整蛊使用fn:substring()会做

<c:forEach var="i" begin="0" end="${fn:length(str)}" step="1"> 
    <c:out value="${fn:substring(str, i, i + 1)}" />  
</c:forEach> 
+0

+1。不错的一个....... :-p – 2011-05-12 12:13:15

+0

@哈里谢谢:) – 2011-05-12 12:13:54

+0

不错 - 不知道有什么功能可用 – 2011-05-12 12:33:06

0

我认为你不能用JSTL的forEach做到这一点。您需要编写自己的标签或EL功能。下面是一个示例代码,你如何编写自定义标签: http://www.java2s.com/Tutorial/Java/0360__JSP/CustomTagSupport.htm

+0

为什么不呢? http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fn/tld-summary.html 我在这里看到一个长度函数和一个子字符串函数 – stivlo 2011-05-12 12:05:24

+0

fn :子字符串/ FN:长度仍然是EL功能! – 2011-05-12 12:06:32

+0

我认为将scriptlet与JSTL混合是一种不好的做法,而EL应该是优雅的。你的意思是EL不是JSTL的一部分吗? – stivlo 2011-05-12 12:10:58

1

迟到了,但EL 2.2允许实例方法调用(更多的在这里: https://stackoverflow.com/a/7122669/2047962)。这意味着,你可以通过几个字符缩短Jigar乔希的回答是:

<c:forEach var="i" begin="0" end="${fn:length(str)}" step="1"> 
    <c:out value="${str.charAt(i)}" />  
</c:forEach> 

我只建议这一点,因为它是一个小更明显你的代码做什么。

相关问题