问题
I'm using JSF 2 and I need c:foreach in some purposes. But no matter how big my list is, c:foreach loops only once, returning empty value. I tried everything, I even isolated c:foreach in separate .xhtml, but it still gives same result. If you need some piece of code, please ask, but I would like to make c:foreach work at least in separate .xhtml, and I supose that than it will work in my code, too.
回答1:
Open the page in the webbrowser, rightclick and View Source. You see the <c:forEach> tag plain vanilla in there, right? It is not been parsed and executed at all? This is not right. You need to ensure that you have declared the JSTL core taglib in the XML namespace in order to get it to be parsed and executed by Facelets. The proper XML namespace declaration is the following:
xmlns:c="http://java.sun.com/jsp/jstl/core"
If you have already done it, then it can only mean that you're running the webapp on a server which does not have JSTL preinstalled. Full fledged Java EE application servers like Glassfish and JBoss AS already ship with JSTL bundled, but simple servlet containers like Tomcat and Jetty not. You'd need to download and put the JSTL library in webapp's /WEB-INF/lib or maybe even in server's own /lib folder.
See also:
- Our JSTL tag wiki page - contains download links and installation instructions
Unrelated to the concrete problem, are you well aware that the <c:forEach> is a view build time tag, not a view render time tag? If you're actually looking for the latter, then you should be using Facelets' own <ui:repeat> tag instead. See also JSTL in JSF2 Facelets... makes sense?
来源:https://stackoverflow.com/questions/8903483/cforeach-returns-only-one-empty-value