1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Aleksandar
  • 3,541
  • 4
  • 34
  • 57

1 Answers1

5

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:


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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Thanks for your answer. Yes, you are right - when I open source code I see foreach tag. I declared jstl like this: xmlns:c="http://java.sun.com/jstl/core", so I tried like you said to put "http://java.sun.com/jsp/jstl/core" and to add jstl jar to lib folder of my server - I use tomcat 7 (it was already in my aplication's lib) but it didn't work. I got error: Tag Library supports namespace: http://java.sun.com/jsp/jstl/core, but no tag was defined for name: foreach javax.faces.webapp.FacesServlet.service(FacesServlet.java:205) – Aleksandar Jan 18 '12 at 17:14
  • Like everything else in Java, the `` tag name is case sensitive. – BalusC Jan 18 '12 at 17:17
  • Can I ask you just one more thing, since I saw you are very good in this. I would be very thankful if you could help me again. So, I have a4j:jsFunction which changes bean's array content and than rerender element in which forEach is placed, and now forEach behaves strange: if I removed one element of that array it will output empty value for that element, and if I add element it will output everything except the new one. So it seems like it knows the previous size of the array or something like that. What I need is to jsFunction executes comletely before forEach. Is it possible? – Aleksandar Jan 18 '12 at 23:44